admin / Apex
publicBid Management and Orchestration Tool with AI Capability
Apex / Synapse-Apexv2 / backend / app / services / storage.py
1740 B · main
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 | """File storage + text extraction. Abstracted so S3/MinIO can be swapped later.""" from __future__ import annotations import hashlib import io import os import uuid from app.core.config import settings def save_file(data: bytes, filename: str) -> tuple[str, str]: os.makedirs(settings.upload_dir, exist_ok=True) ext = os.path.splitext(filename)[1] stored = f"{uuid.uuid4().hex}{ext}" path = os.path.join(settings.upload_dir, stored) with open(path, "wb") as fh: fh.write(data) sha = hashlib.sha256(data).hexdigest() return path, sha def extract_text(data: bytes, filename: str, mime: str) -> str: name = filename.lower() try: if name.endswith(".pdf"): from pypdf import PdfReader reader = PdfReader(io.BytesIO(data)) return "\n".join((p.extract_text() or "") for p in reader.pages) if name.endswith(".docx"): from docx import Document as Docx doc = Docx(io.BytesIO(data)) return "\n".join(p.text for p in doc.paragraphs) if name.endswith(".xlsx"): from openpyxl import load_workbook wb = load_workbook(io.BytesIO(data), read_only=True, data_only=True) out = [] for ws in wb.worksheets: for row in ws.iter_rows(values_only=True): cells = [str(c) for c in row if c is not None] if cells: out.append("\t".join(cells)) return "\n".join(out) if name.endswith((".txt", ".md")): return data.decode("utf-8", errors="replace") except Exception as exc: # pragma: no cover return f"[text extraction failed: {exc}]" return "" |