47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from fastapi import APIRouter, HTTPException, Response, status
|
|
|
|
from app.services import engine
|
|
from app.services.export import apply_need_appearances
|
|
from app.services.store import document_store
|
|
|
|
router = APIRouter(tags=["documents"])
|
|
|
|
@router.get("/{document_id}/export")
|
|
def export_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.",
|
|
)
|
|
|
|
d = document_store.get_document(document_id)
|
|
if not d:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
|
|
|
|
perms = d.get("permissions") or {}
|
|
if perms.get("canCopy", True) is False:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Exporting is not permitted by this document's restrictions (canCopy).",
|
|
)
|
|
|
|
try:
|
|
doc = d["doc_instance"]
|
|
bytes_data = doc.save_full()
|
|
filename = d["filename"]
|
|
if not filename.endswith(".pdf"):
|
|
filename += ".pdf"
|
|
|
|
bytes_data = apply_need_appearances(bytes_data)
|
|
|
|
return Response(
|
|
content=bytes_data,
|
|
media_type="application/pdf",
|
|
headers={
|
|
"Content-Disposition": f'attachment; filename="{filename}"',
|
|
"Content-Length": str(len(bytes_data)),
|
|
},
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
|