59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from fastapi import APIRouter, HTTPException, status
|
|
|
|
from app.schemas.compare import CompareRequest, CompareResponse
|
|
from app.services import engine
|
|
from app.services.compare import compare_documents
|
|
from app.services.store import document_store
|
|
|
|
router = APIRouter(prefix="/documents", tags=["compare"])
|
|
|
|
|
|
@router.post("/compare", response_model=CompareResponse)
|
|
async def compare_documents_endpoint(req: CompareRequest) -> CompareResponse:
|
|
if not engine.is_available():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
detail="Engine bridge (bindings/python) not yet available.",
|
|
)
|
|
|
|
docA_info = document_store.get_document(req.documentIdA)
|
|
if not docA_info:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Document A not found.",
|
|
)
|
|
|
|
docB_info = document_store.get_document(req.documentIdB)
|
|
if not docB_info:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Document B not found.",
|
|
)
|
|
|
|
permsA = docA_info.get("permissions") or {}
|
|
if permsA.get("isEncrypted", False) and not docA_info.get("password") and permsA.get("canCopy") is False:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Password required to open Document A.",
|
|
)
|
|
|
|
permsB = docB_info.get("permissions") or {}
|
|
if permsB.get("isEncrypted", False) and not docB_info.get("password") and permsB.get("canCopy") is False:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Password required to open Document B.",
|
|
)
|
|
|
|
try:
|
|
return compare_documents(
|
|
docA_info,
|
|
docB_info,
|
|
include_visual_diff=req.includeVisualDiff,
|
|
dpi=req.dpi,
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to compare documents: {e!s}",
|
|
)
|