Update app/routes/files.py
Browse files- app/routes/files.py +23 -4
app/routes/files.py
CHANGED
|
@@ -25,14 +25,33 @@ async def list_files():
|
|
| 25 |
}
|
| 26 |
|
| 27 |
@router.get("/download")
|
| 28 |
-
async def download_file_by_name(filename: str = Query(..., description="The exact name of the
|
| 29 |
"""
|
| 30 |
-
Takes a file name (e.g., 'document.pdf'
|
| 31 |
-
download URL for its generated
|
| 32 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# 1. Find the job associated with this filename
|
| 34 |
-
job = await run_in_threadpool(get_job_by_filename,
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if not job:
|
| 37 |
raise HTTPException(status_code=404, detail=f"File '{filename}' not found in records.")
|
| 38 |
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
@router.get("/download")
|
| 28 |
+
async def download_file_by_name(filename: str = Query(..., description="The exact name of the file")):
|
| 29 |
"""
|
| 30 |
+
Takes a file name (e.g., 'document.pdf' or 'document_report.md')
|
| 31 |
+
and returns the download URL for its generated report.
|
| 32 |
"""
|
| 33 |
+
|
| 34 |
+
# --- FIX START: Handle Filename Mismatch ---
|
| 35 |
+
search_name = filename
|
| 36 |
+
|
| 37 |
+
# If frontend asks for '..._report.md', revert it to '.pdf' to search the DB
|
| 38 |
+
if filename.endswith("_report.md"):
|
| 39 |
+
search_name = filename.replace("_report.md", ".pdf")
|
| 40 |
+
# -------------------------------------------
|
| 41 |
+
|
| 42 |
# 1. Find the job associated with this filename
|
| 43 |
+
job = await run_in_threadpool(get_job_by_filename, search_name)
|
| 44 |
|
| 45 |
+
# If not found via PDF logic, it might be a ZIP batch that didn't get renamed
|
| 46 |
+
if not job and filename.endswith("_report.md"):
|
| 47 |
+
# Try replacing with .zip just in case it was a batch report
|
| 48 |
+
search_name_zip = filename.replace("_report.md", ".zip")
|
| 49 |
+
job = await run_in_threadpool(get_job_by_filename, search_name_zip)
|
| 50 |
+
|
| 51 |
+
if not job:
|
| 52 |
+
# Final fallback: maybe the user really uploaded a file named '..._report.md'
|
| 53 |
+
job = await run_in_threadpool(get_job_by_filename, filename)
|
| 54 |
+
|
| 55 |
if not job:
|
| 56 |
raise HTTPException(status_code=404, detail=f"File '{filename}' not found in records.")
|
| 57 |
|