"""Document upload and management endpoints.""" import asyncio from fastapi import APIRouter, File, HTTPException, UploadFile router = APIRouter(prefix="/api/documents") def get_ingestion_service(): from core.bootstrap import get_ingestion_service as _get return _get() def get_db(): from core.bootstrap import get_db as _get return _get() @router.post("/upload") async def upload_document(file: UploadFile = File(...)): ingestion_service = get_ingestion_service() content = await file.read() filename = file.filename or "unknown" ext = filename.lower().rsplit(".", 1)[-1] if ext == "pdf": result = await asyncio.to_thread(ingestion_service.ingest_pdf, content, filename) elif ext in ("png", "jpg", "jpeg", "tiff", "bmp", "webp"): result = await asyncio.to_thread(ingestion_service.ingest_image, content, filename) else: raise HTTPException( status_code=400, detail="Unsupported file type. Use PDF or image files.", ) return result @router.get("/list") async def list_documents(): db = get_db() return {"documents": db.list_documents()} @router.delete("/{doc_id}") async def delete_document(doc_id: str): db = get_db() db.delete_document(doc_id) return {"status": "deleted", "document_id": doc_id}