Files
pdf/gateway/app/routers/documents/metadata.py
T
2026-06-29 10:51:04 +05:30

57 lines
2.1 KiB
Python

from fastapi import APIRouter, HTTPException, status
from app.schemas.document import DocumentMetadataResponse, OutlineItemResponse
from app.services import engine
from app.services.store import document_store
router = APIRouter(tags=["documents"])
@router.get("/{document_id}/metadata", response_model=DocumentMetadataResponse)
def get_document_metadata(document_id: str) -> DocumentMetadataResponse:
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")
try:
doc = d["doc_instance"]
meta = doc.metadata
return DocumentMetadataResponse(
title=meta.title,
author=meta.author,
creator=meta.creator,
producer=meta.producer,
creation_date=meta.creation_date,
modification_date=meta.modification_date,
)
except Exception as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
@router.get("/{document_id}/outline", response_model=list[OutlineItemResponse])
def get_document_outline(document_id: str) -> list[OutlineItemResponse]:
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")
try:
doc = d["doc_instance"]
items = doc.extract_outline()
return [
OutlineItemResponse(title=it["title"], pageIndex=it["pageIndex"], level=it["level"])
for it in items
]
except Exception as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))