From 82ffe8b6232c5aaeb837740f1a0877926f112013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Mon, 2 Mar 2026 14:27:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/file_manager.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/routes/file_manager.py b/routes/file_manager.py index 5326fb1..91e0138 100644 --- a/routes/file_manager.py +++ b/routes/file_manager.py @@ -164,7 +164,7 @@ async def list_files(path: str = "", recursive: bool = False): async def upload_file(file: UploadFile = File(...), path: str = Form("")): """ 上传文件 - + Args: file: 上传的文件 path: 目标路径(相对于支持目录) @@ -174,26 +174,29 @@ async def upload_file(file: UploadFile = File(...), path: str = Form("")): if not target_path.exists() or not target_path.is_dir(): target_path = resolve_path("projects") target_path.mkdir(parents=True, exist_ok=True) - - file_path = target_path / file.filename - + + # 将文件名中的空格替换为下划线 + safe_filename = file.filename.replace(" ", "_") if file.filename else file.filename + file_path = target_path / safe_filename + # 如果文件已存在,检查是否覆盖 if file_path.exists(): # 可以添加版本控制或重命名逻辑 pass - + with open(file_path, "wb") as buffer: content = await file.read() buffer.write(content) - + return { "success": True, "message": "文件上传成功", - "filename": file.filename, - "path": str(Path(path) / file.filename), + "filename": safe_filename, + "original_filename": file.filename, + "path": str(Path(path) / safe_filename), "size": len(content) } - + except Exception as e: raise HTTPException(status_code=500, detail=f"文件上传失败: {str(e)}")