2026-05-22 15:47:48 +05:30
|
|
|
from typing import List
|
|
|
|
|
from fastapi import APIRouter, HTTPException, status, File, UploadFile
|
|
|
|
|
from pydantic import BaseModel
|
2026-05-16 11:01:20 +05:30
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
from app.services import engine
|
|
|
|
|
from app.services.store import document_store
|
2026-05-16 11:01:20 +05:30
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/documents", tags=["documents"])
|
|
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
class DocumentInfoResponse(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
filename: str
|
|
|
|
|
sizeBytes: int
|
|
|
|
|
totalPages: int
|
|
|
|
|
uploadedAt: str
|
|
|
|
|
status: str
|
2026-05-16 11:01:20 +05:30
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
@router.post("", response_model=DocumentInfoResponse, status_code=status.HTTP_201_CREATED)
|
|
|
|
|
async def upload_document(file: UploadFile = File(...), password: str = "") -> DocumentInfoResponse:
|
|
|
|
|
if not engine.is_available():
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
|
detail="Engine bridge (bindings/python) not yet available."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bytes_data = await file.read()
|
|
|
|
|
try:
|
|
|
|
|
pdfengine = engine.require()
|
|
|
|
|
doc = pdfengine.PdfDocument.load_from_memory(bytes_data, password)
|
|
|
|
|
info = document_store.add_document(file.filename, bytes_data, doc)
|
|
|
|
|
return DocumentInfoResponse(
|
|
|
|
|
id=info["id"],
|
|
|
|
|
filename=info["filename"],
|
|
|
|
|
sizeBytes=info["sizeBytes"],
|
|
|
|
|
totalPages=info["totalPages"],
|
|
|
|
|
uploadedAt=info["uploadedAt"],
|
|
|
|
|
status=info["status"]
|
|
|
|
|
)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
detail = str(e)
|
|
|
|
|
if "Password required" in detail:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Password required")
|
|
|
|
|
elif "Invalid password" in detail:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password")
|
|
|
|
|
else:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Failed to load PDF: {str(e)}")
|
2026-05-16 11:01:20 +05:30
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
@router.get("", response_model=List[DocumentInfoResponse])
|
|
|
|
|
def list_documents() -> List[DocumentInfoResponse]:
|
|
|
|
|
if not engine.is_available():
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
|
detail="Engine bridge (bindings/python) not yet available."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
docs = document_store.list_documents()
|
|
|
|
|
return [
|
|
|
|
|
DocumentInfoResponse(
|
|
|
|
|
id=d["id"],
|
|
|
|
|
filename=d["filename"],
|
|
|
|
|
sizeBytes=d["sizeBytes"],
|
|
|
|
|
totalPages=d["totalPages"],
|
|
|
|
|
uploadedAt=d["uploadedAt"],
|
|
|
|
|
status=d["status"]
|
|
|
|
|
)
|
|
|
|
|
for d in docs
|
|
|
|
|
]
|
2026-05-16 11:01:20 +05:30
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
@router.get("/{document_id}", response_model=DocumentInfoResponse)
|
|
|
|
|
def get_document(document_id: str) -> DocumentInfoResponse:
|
|
|
|
|
if not engine.is_available():
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
|
detail="Engine bridge (bindings/python) not yet available."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
d = document_store.get_document(document_id)
|
|
|
|
|
if not d:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
|
|
|
|
|
|
|
|
|
|
return DocumentInfoResponse(
|
|
|
|
|
id=d["id"],
|
|
|
|
|
filename=d["filename"],
|
|
|
|
|
sizeBytes=d["sizeBytes"],
|
|
|
|
|
totalPages=d["totalPages"],
|
|
|
|
|
uploadedAt=d["uploadedAt"],
|
|
|
|
|
status=d["status"]
|
|
|
|
|
)
|
2026-05-16 11:01:20 +05:30
|
|
|
|
2026-05-22 15:47:48 +05:30
|
|
|
@router.delete("/{document_id}")
|
|
|
|
|
def delete_document(document_id: str):
|
|
|
|
|
if not engine.is_available():
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
|
detail="Engine bridge (bindings/python) not yet available."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
deleted = document_store.delete_document(document_id)
|
|
|
|
|
if not deleted:
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
|
|
|
|
|
|
|
|
|
|
return {"success": True}
|