File size: 1,519 Bytes
3a5abb3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import os
import re
import fitz # PyMuPDF for reading PDFs
from typing import List
def extract_text_from_pdf(file_path: str) -> str:
"""
Extracts plain text from a PDF file using PyMuPDF.
"""
doc = fitz.open(file_path)
text = ""
for page in doc:
text += page.get_text() # Extract text from each page
return text
def read_log_file(file_path: str) -> str:
"""
Reads a .log or .txt file and returns its content as text.
"""
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
return f.read()
def detect_log_format(text: str) -> str:
"""
Detects the log format using basic pattern matching.
Returns one of: 'syslog', 'json', 'cef', or 'unknown'.
"""
if re.search(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", text):
return "syslog"
elif re.search(r"\{.*\}", text):
return "json"
elif "CEF:" in text:
return "cef"
else:
return "unknown"
def parse_uploaded_files(files: List[str]) -> str:
"""
Accepts a list of uploaded files, extracts content from each,
and returns all logs as one combined string.
"""
all_logs = ""
for file_obj in files:
file_path = file_obj.name
if file_path.endswith('.pdf'):
all_logs += extract_text_from_pdf(file_path) + "\n"
elif file_path.endswith(('.log', '.txt')):
all_logs += read_log_file(file_path) + "\n"
return all_logs
|