From 3eb477161ee70442fb44ed26970d133706d4f72a Mon Sep 17 00:00:00 2001 From: mxr612 Date: Tue, 17 Jun 2025 16:04:23 +0800 Subject: [PATCH] feat: enhance file download response headers in download_scale_results endpoint - Updated the download_scale_results function to return additional cache control headers for the "psychoscales.db" file download, improving client-side caching behavior. - Ensured consistent response headers for CSV file downloads, enhancing the overall file handling experience. --- app.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 3d046b3..a92e087 100644 --- a/app.py +++ b/app.py @@ -267,10 +267,17 @@ async def result(request: Request, scale_id: str, db: Session = Depends(get_db)) @app.get("/download/{scale_id}") async def download_scale_results(scale_id: str, db: Session = Depends(get_db)): - if scale_id == "psychoscales.db": + if scale_id == "database": public_path = os.path.join("psychoscales.db") if os.path.isfile(public_path): - return FileResponse(public_path) + return FileResponse( + "psychoscales.db", + headers={ + "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0", + "Pragma": "no-cache", + "Expires": "0" + } + ) raise HTTPException(status_code=404, detail="File not found") # Get all responses for this scale @@ -340,7 +347,10 @@ async def download_scale_results(scale_id: str, db: Session = Depends(get_db)): content=output.getvalue(), media_type="text/csv", headers={ - "Content-Disposition": f'attachment; filename="{scale_id}_responses.csv"' + "Content-Disposition": f'attachment; filename="{scale_id}_responses.csv"', + "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0", + "Pragma": "no-cache", + "Expires": "0" } )