File size: 1,145 Bytes
4ea7f74
97e667f
 
327b929
958fece
 
 
 
97e667f
4ea7f74
958fece
4ea7f74
 
958fece
 
4ea7f74
 
327b929
 
 
 
 
 
 
958fece
327b929
 
958fece
 
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
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)}")