From 53351c86f3d53167d722e6e0c9fe89b078f2876b Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 25 Jan 2026 09:04:58 -0600 Subject: [PATCH] Fix PDF pages route ordering for correct path matching Move /files/{storage_key}/pages endpoint before the base /files/{storage_key} endpoint so FastAPI matches the more specific route first. Co-Authored-By: Claude Opus 4.5 --- backend/app/api/routes.py | 59 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py index 2a46c4c..409b242 100755 --- a/backend/app/api/routes.py +++ b/backend/app/api/routes.py @@ -647,35 +647,7 @@ async def list_agencies( return [AgencyResponse(id=a.id, name=a.name) for a in agencies] -# File download endpoint -@router.get("/files/{storage_key:path}") -async def get_file( - storage_key: str, - db: AsyncSession = Depends(get_db), - user: dict = Depends(get_current_user), -): - """Retrieve a stored file by its storage key.""" - file_data = await storage_service.get_file(storage_key) - if file_data is None: - raise HTTPException(status_code=404, detail="File not found") - - # Determine content type from extension - extension = storage_key.split('.')[-1].lower() if '.' in storage_key else '' - content_types = { - 'png': 'image/png', - 'jpg': 'image/jpeg', - 'jpeg': 'image/jpeg', - 'gif': 'image/gif', - 'webp': 'image/webp', - 'svg': 'image/svg+xml', - 'pdf': 'application/pdf', - } - return Response( - content=file_data, - media_type=content_types.get(extension, 'application/octet-stream'), - ) - - +# PDF pages endpoint (must be defined BEFORE the base file endpoint for correct routing) @router.get("/files/{storage_key:path}/pages") async def get_pdf_pages( storage_key: str, @@ -709,6 +681,35 @@ async def get_pdf_pages( } +# File download endpoint +@router.get("/files/{storage_key:path}") +async def get_file( + storage_key: str, + db: AsyncSession = Depends(get_db), + user: dict = Depends(get_current_user), +): + """Retrieve a stored file by its storage key.""" + file_data = await storage_service.get_file(storage_key) + if file_data is None: + raise HTTPException(status_code=404, detail="File not found") + + # Determine content type from extension + extension = storage_key.split('.')[-1].lower() if '.' in storage_key else '' + content_types = { + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', + 'webp': 'image/webp', + 'svg': 'image/svg+xml', + 'pdf': 'application/pdf', + } + return Response( + content=file_data, + media_type=content_types.get(extension, 'application/octet-stream'), + ) + + # Support email endpoint (public - no auth required for login page access) @router.post("/support/email") async def send_support_email(