fix: correct public file path handling in get_public_file function

- Moved the public_path assignment to ensure it is set before checking for the file's existence.
- This change resolves potential issues when accessing files in the public directory.
This commit is contained in:
Miu Li 2025-06-16 09:32:23 +08:00
parent fc9d439cf9
commit d99d7f7c12

2
app.py
View File

@ -197,12 +197,12 @@ def generate_sitemap():
# Mount all files from public directory to root # Mount all files from public directory to root
@app.get("/{filename}") @app.get("/{filename}")
async def get_public_file(filename: str): async def get_public_file(filename: str):
public_path = os.path.join("public", filename)
if filename == "sitemap.xml": if filename == "sitemap.xml":
return Response( return Response(
content=generate_sitemap(), content=generate_sitemap(),
media_type="application/xml" media_type="application/xml"
) )
public_path = os.path.join("public", filename)
if os.path.isfile(public_path): if os.path.isfile(public_path):
return FileResponse(public_path) return FileResponse(public_path)
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")