From 51f988e5353c6d62cdd85f9344638a1a266093a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Fri, 30 Jan 2026 00:03:26 +0800 Subject: [PATCH] =?UTF-8?q?update=20sql=E8=A1=A8=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/file_manager.py | 44 ++++++++++++++++++++++++++++++++---------- utils/fastapi_utils.py | 8 ++++---- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/routes/file_manager.py b/routes/file_manager.py index 5326fb1..f5b8813 100644 --- a/routes/file_manager.py +++ b/routes/file_manager.py @@ -164,36 +164,60 @@ async def list_files(path: str = "", recursive: bool = False): async def upload_file(file: UploadFile = File(...), path: str = Form("")): """ 上传文件 - + Args: file: 上传的文件 - path: 目标路径(相对于支持目录) + path: 目标路径(相对于支持目录),如果不存在会自动创建 """ try: - target_path = resolve_path(path) if path else resolve_path("projects") - if not target_path.exists() or not target_path.is_dir(): + # 如果没有指定路径,默认使用 projects 目录 + if not path: target_path = resolve_path("projects") + else: + # 验证路径格式是否合法(必须以支持的目录开头) + path_parts = Path(path).parts + if not path_parts or path_parts[0] not in SUPPORTED_DIRECTORIES: + raise HTTPException( + status_code=400, + detail=f"路径必须以以下目录之一开头: {', '.join(SUPPORTED_DIRECTORIES)}" + ) + # 直接构建路径,不检查是否存在(稍后会创建) + target_path = PROJECTS_DIR / path + + # 如果目标路径已存在且是文件,则使用其父目录 + if target_path.exists() and target_path.is_file(): + target_path = target_path.parent + + # 创建目标目录(包括所有不存在的父目录) target_path.mkdir(parents=True, exist_ok=True) - + file_path = target_path / file.filename - + # 如果文件已存在,检查是否覆盖 if file_path.exists(): # 可以添加版本控制或重命名逻辑 pass - + with open(file_path, "wb") as buffer: content = await file.read() buffer.write(content) - + + # 计算返回的相对路径 + try: + relative_path = file_path.relative_to(PROJECTS_DIR) + except ValueError: + relative_path = file_path + return { "success": True, "message": "文件上传成功", "filename": file.filename, - "path": str(Path(path) / file.filename), + "path": str(relative_path), "size": len(content) } - + + except HTTPException: + raise except Exception as e: raise HTTPException(status_code=500, detail=f"文件上传失败: {str(e)}") diff --git a/utils/fastapi_utils.py b/utils/fastapi_utils.py index fb1ef35..125a8da 100644 --- a/utils/fastapi_utils.py +++ b/utils/fastapi_utils.py @@ -465,7 +465,7 @@ async def fetch_bot_config_from_db(bot_user_id: str) -> Dict[str, Any]: async with conn.cursor() as cursor: # 首先根据 bot_user_id 查找 bot 的 UUID await cursor.execute( - "SELECT id, name FROM bots WHERE bot_id = %s", + "SELECT id, name FROM agent_bots WHERE bot_id = %s", (bot_user_id,) ) bot_row = await cursor.fetchone() @@ -484,7 +484,7 @@ async def fetch_bot_config_from_db(bot_user_id: str) -> Dict[str, Any]: SELECT model_id, language, robot_type, dataset_ids, system_prompt, user_identifier, enable_memori, tool_response, skills - FROM bot_settings WHERE bot_id = %s + FROM agent_bot_settings WHERE bot_id = %s """, (bot_uuid,) ) @@ -521,7 +521,7 @@ async def fetch_bot_config_from_db(bot_user_id: str) -> Dict[str, Any]: await cursor.execute( """ SELECT model, server, api_key - FROM models WHERE id = %s + FROM agent_models WHERE id = %s """, (model_id,) ) @@ -570,7 +570,7 @@ async def fetch_bot_config_from_db(bot_user_id: str) -> Dict[str, Any]: await cursor.execute( """ SELECT name, type, config, enabled - FROM mcp_servers WHERE bot_id = %s AND enabled = true + FROM agent_mcp_servers WHERE bot_id = %s AND enabled = true """, (bot_uuid,) )