From 693f6a90ad1a89571815e9a8948fff3581b4d52f Mon Sep 17 00:00:00 2001 From: Feige-cn Date: Thu, 13 Mar 2025 11:06:49 +0800 Subject: [PATCH] fix download bug --- app.py | 16 +++++++++++++++- run.bat | 2 +- static/main.js | 10 +++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 5194732..d314508 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import asyncio +import os import threading import tomllib import uuid @@ -10,7 +11,12 @@ from pathlib import Path from fastapi import Body, FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware -from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse +from fastapi.responses import ( + FileResponse, + HTMLResponse, + JSONResponse, + StreamingResponse, +) from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from pydantic import BaseModel @@ -93,6 +99,14 @@ async def index(request: Request): return templates.TemplateResponse("index.html", {"request": request}) +@app.get("/download") +async def download_file(file_path: str): + if not os.path.exists(file_path): + raise HTTPException(status_code=404, detail="File not found") + + return FileResponse(file_path, filename=os.path.basename(file_path)) + + @app.post("/tasks") async def create_task(prompt: str = Body(..., embed=True)): task = task_manager.create_task(prompt) diff --git a/run.bat b/run.bat index d9272db..b5afb55 100644 --- a/run.bat +++ b/run.bat @@ -6,7 +6,7 @@ set "VENV_DIR=%~dp0venv" set "PYTHON_PATH=%VENV_DIR%\python.exe" where git >nul 2>&1 -if !errorlevel! == 0 ( +if %errorlevel% == 0 ( echo Trying to sync with GitHub repository... git pull origin front-end 2>&1 || echo Failed to sync with GitHub, skipping update... ) else ( diff --git a/static/main.js b/static/main.js index 9bfd2f3..4fd8518 100644 --- a/static/main.js +++ b/static/main.js @@ -286,27 +286,27 @@ function createStepElement(type, content, timestamp) { fileInteractionHtml = `
${fileName} - ⬇️ 下载图片 + ⬇️ 下载图片
`; } else if (['mp3', 'wav', 'ogg'].includes(fileExtension)) { fileInteractionHtml = `
- ⬇️ 下载音频 + ⬇️ 下载音频
`; - } else if (fileExtension === 'py') { + } else if (['html', 'js', 'py'].includes(fileExtension)) { fileInteractionHtml = `
- ⬇️ 下载文件 + ⬇️ 下载文件
`; } else { fileInteractionHtml = `
- ⬇️ 下载文件: ${fileName} + ⬇️ 下载文件: ${fileName}
`; }