rahimizadeh's picture
Upload 3 files
3a5abb3 verified
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