File size: 918 Bytes
1b64148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile

from api.deps import get_extractor
from domain.extractor import TextExtractor
from services.detect import sniff_kind
from services.files import cleanup, save_upload_to_temp

router = APIRouter(tags=["texto"])


@router.post("/extract-text")
async def extract_text(

    file: UploadFile = File(...),

    extractor: TextExtractor = Depends(get_extractor),

):
    """Recebe PDF, JPG ou PNG e devolve o texto extraido (cru, sem tratamento)."""
    src = await save_upload_to_temp(file)
    try:
        kind = sniff_kind(src)
        if kind == "unknown":
            raise HTTPException(
                status_code=415,
                detail="Formato nao suportado. Envie PDF, JPG ou PNG.",
            )
        text = extractor.extract(src, kind=kind)
        return {"text": text}
    finally:
        cleanup(src)