Skip to content

Commit 38e3421

Browse files
authored
Merge pull request #565 from Feige-cn/front-end
fix download bug
2 parents d847b55 + 693f6a9 commit 38e3421

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

app.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import os
23
import threading
34
import tomllib
45
import uuid
@@ -10,7 +11,12 @@
1011

1112
from fastapi import Body, FastAPI, HTTPException, Request
1213
from fastapi.middleware.cors import CORSMiddleware
13-
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
14+
from fastapi.responses import (
15+
FileResponse,
16+
HTMLResponse,
17+
JSONResponse,
18+
StreamingResponse,
19+
)
1420
from fastapi.staticfiles import StaticFiles
1521
from fastapi.templating import Jinja2Templates
1622
from pydantic import BaseModel
@@ -93,6 +99,14 @@ async def index(request: Request):
9399
return templates.TemplateResponse("index.html", {"request": request})
94100

95101

102+
@app.get("/download")
103+
async def download_file(file_path: str):
104+
if not os.path.exists(file_path):
105+
raise HTTPException(status_code=404, detail="File not found")
106+
107+
return FileResponse(file_path, filename=os.path.basename(file_path))
108+
109+
96110
@app.post("/tasks")
97111
async def create_task(prompt: str = Body(..., embed=True)):
98112
task = task_manager.create_task(prompt)

run.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set "VENV_DIR=%~dp0venv"
66
set "PYTHON_PATH=%VENV_DIR%\python.exe"
77

88
where git >nul 2>&1
9-
if !errorlevel! == 0 (
9+
if %errorlevel% == 0 (
1010
echo Trying to sync with GitHub repository...
1111
git pull origin front-end 2>&1 || echo Failed to sync with GitHub, skipping update...
1212
) else (

static/main.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,27 @@ function createStepElement(type, content, timestamp) {
286286
fileInteractionHtml = `
287287
<div class="file-interaction image-preview">
288288
<img src="${filePath}" alt="${fileName}" class="preview-image" onclick="showFullImage('${filePath}')">
289-
<a href="${filePath}" download="${fileName}" class="download-link">⬇️ 下载图片</a>
289+
<a href="/download?file_path=${filePath}" download="${fileName}" class="download-link">⬇️ 下载图片</a>
290290
</div>
291291
`;
292292
} else if (['mp3', 'wav', 'ogg'].includes(fileExtension)) {
293293
fileInteractionHtml = `
294294
<div class="file-interaction audio-player">
295295
<audio controls src="${filePath}"></audio>
296-
<a href="${filePath}" download="${fileName}" class="download-link">⬇️ 下载音频</a>
296+
<a href="/download?file_path=${filePath}" download="${fileName}" class="download-link">⬇️ 下载音频</a>
297297
</div>
298298
`;
299-
} else if (fileExtension === 'py') {
299+
} else if (['html', 'js', 'py'].includes(fileExtension)) {
300300
fileInteractionHtml = `
301301
<div class="file-interaction code-file">
302302
<button onclick="simulateRunPython('${filePath}')" class="run-button">▶️ 模拟运行</button>
303-
<a href="${filePath}" download="${fileName}" class="download-link">⬇️ 下载文件</a>
303+
<a href="/download?file_path=${filePath}" download="${fileName}" class="download-link">⬇️ 下载文件</a>
304304
</div>
305305
`;
306306
} else {
307307
fileInteractionHtml = `
308308
<div class="file-interaction">
309-
<a href="${filePath}" download="${fileName}" class="download-link">⬇️ 下载文件: ${fileName}</a>
309+
<a href="/download?file_path=${filePath}" download="${fileName}" class="download-link">⬇️ 下载文件: ${fileName}</a>
310310
</div>
311311
`;
312312
}

0 commit comments

Comments
 (0)