63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
"""Inbox helpers — attachment loading and other non-routing checks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from pathlib import Path
|
|
|
|
from inbox.models import Inbox_Messages
|
|
from job.candidate.views import FileRead
|
|
|
|
|
|
def load_message_files(message: Inbox_Messages) -> list[dict]:
|
|
"""Read files from file_path when they exist on disk."""
|
|
if not message.file_path:
|
|
return []
|
|
|
|
files: list[dict] = []
|
|
for path_str in message.file_path.split(","):
|
|
path = Path(path_str.strip())
|
|
if not path.is_file():
|
|
continue
|
|
try:
|
|
raw = path.read_bytes()
|
|
except OSError:
|
|
continue
|
|
files.append(
|
|
{
|
|
"file_name": path.name,
|
|
"content_base64": base64.b64encode(raw).decode("ascii"),
|
|
"size": len(raw),
|
|
}
|
|
)
|
|
return files
|
|
|
|
|
|
async def extract_resume_text(file_paths: list[str]) -> tuple[str, str]:
|
|
"""Extract text from the PDFs among file_paths. Returns (combined_text, error)."""
|
|
pdf_paths = [
|
|
Path(p.strip())
|
|
for p in (file_paths or [])
|
|
if p and p.strip() and Path(p.strip()).suffix.lower() == ".pdf"
|
|
]
|
|
existing = [p for p in pdf_paths if p.is_file()]
|
|
if not existing:
|
|
return "", "no PDF attachment to extract (.doc/.docx not supported)"
|
|
|
|
texts: list[str] = []
|
|
errors: list[str] = []
|
|
for path in existing:
|
|
try:
|
|
raw = path.read_bytes()
|
|
result = await FileRead(session=None, filename=path.name, file=raw).read_file()
|
|
text = (result.get("text") or "").strip()
|
|
if text:
|
|
texts.append(text)
|
|
except Exception as exc:
|
|
errors.append(f"{path.name}: {exc}")
|
|
|
|
if not texts:
|
|
return "", "; ".join(errors) if errors else "no text extracted from PDF"
|
|
|
|
return "\n\n---\n\n".join(texts), ""
|