Spaces:
Sleeping
Sleeping
Update app/api/documents.py
Browse files- app/api/documents.py +6 -2
app/api/documents.py
CHANGED
|
@@ -61,20 +61,24 @@ def get_document_file(
|
|
| 61 |
print(f"ERROR: File download failed for path: {document.file_path}")
|
| 62 |
raise HTTPException(status_code=404, detail=f"File not found in cloud storage: {document.file_path}")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
return StreamingResponse(
|
| 65 |
io.BytesIO(content),
|
| 66 |
media_type="application/pdf",
|
| 67 |
-
headers={"Content-Disposition": f"inline; filename={
|
| 68 |
)
|
| 69 |
|
| 70 |
# Fallback to local
|
| 71 |
if not document.file_path or not os.path.exists(document.file_path):
|
| 72 |
raise HTTPException(status_code=404, detail="File not found")
|
| 73 |
|
|
|
|
| 74 |
return FileResponse(
|
| 75 |
path=document.file_path,
|
| 76 |
media_type="application/pdf",
|
| 77 |
-
filename=
|
| 78 |
)
|
| 79 |
|
| 80 |
|
|
|
|
| 61 |
print(f"ERROR: File download failed for path: {document.file_path}")
|
| 62 |
raise HTTPException(status_code=404, detail=f"File not found in cloud storage: {document.file_path}")
|
| 63 |
|
| 64 |
+
# Sanitize filename for HTTP headers (remove non-latin-1 characters like special dashes)
|
| 65 |
+
safe_header_filename = document.filename.encode('ascii', 'ignore').decode('ascii')
|
| 66 |
+
|
| 67 |
return StreamingResponse(
|
| 68 |
io.BytesIO(content),
|
| 69 |
media_type="application/pdf",
|
| 70 |
+
headers={"Content-Disposition": f"inline; filename=\"{safe_header_filename}\""}
|
| 71 |
)
|
| 72 |
|
| 73 |
# Fallback to local
|
| 74 |
if not document.file_path or not os.path.exists(document.file_path):
|
| 75 |
raise HTTPException(status_code=404, detail="File not found")
|
| 76 |
|
| 77 |
+
safe_header_filename = document.filename.encode('ascii', 'ignore').decode('ascii')
|
| 78 |
return FileResponse(
|
| 79 |
path=document.file_path,
|
| 80 |
media_type="application/pdf",
|
| 81 |
+
filename=safe_header_filename
|
| 82 |
)
|
| 83 |
|
| 84 |
|