66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
"""Inbox helpers — attachment loading and resume text extraction."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from pathlib import Path
|
|
|
|
from inbox.models import Inbox_Messages
|
|
from job.candidate.views import FileRead
|
|
|
|
_ATTACHMENTS_DIR=Path(__file__).resolve().parent/"decoded_attachments"
|
|
|
|
|
|
def resolve_attachment_path(path_str:str) -> Path:
|
|
"""Prefer stored path; fall back to basename under decoded_attachments."""
|
|
path=Path(path_str.strip())
|
|
if path.is_file():
|
|
return path
|
|
fallback=_ATTACHMENTS_DIR/path.name
|
|
if fallback.is_file():
|
|
return fallback
|
|
return path
|
|
|
|
|
|
def load_message_files(message:Inbox_Messages) -> list[dict]:
|
|
if not message.file_path:
|
|
return []
|
|
files=[]
|
|
for path_str in message.file_path.split(","):
|
|
path=resolve_attachment_path(path_str)
|
|
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]:
|
|
candidates=[resolve_attachment_path(p) for p in (file_paths or []) if p and p.strip()]
|
|
existing=[p for p in candidates if p.is_file() and p.suffix.lower()==".pdf"]
|
|
if not existing:
|
|
return "","no PDF attachment to extract (.doc/.docx not supported)"
|
|
|
|
texts=[]
|
|
errors=[]
|
|
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),""
|