import io from fastapi import APIRouter, Depends, HTTPException, File, UploadFile from services.auth import require_service_key from services.pdf_extractor import extract_docx_from_stream, extract_resume_from_stream router = APIRouter(prefix="/extract-resume", tags=["extraction"]) @router.post("", dependencies=[Depends(require_service_key)]) async def extract_resume(file: UploadFile = File(...)): """ Receives an uploaded PDF file stream, extracts and structures its text, and returns it to NestJS. """ try: content = await file.read() pdf_stream = io.BytesIO(content) file_name = (file.filename or '').lower() if file_name.endswith('.docx'): result = extract_docx_from_stream(pdf_stream) elif file_name.endswith('.pdf'): result = extract_resume_from_stream(pdf_stream) else: raise HTTPException(status_code=400, detail='Only PDF and DOCX files are supported.') return result except HTTPException: raise except Exception as e: raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")