From 6003563adb76b8a16ffc43706e1bcdf8947ad488 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Sun, 7 Dec 2025 18:25:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(storage):=20=E5=B0=86=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=B8=BA=E5=BC=82=E6=AD=A5=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构图片上传功能为通用的异步文件上传,支持自定义文件名 移除同步上传方法,统一使用异步接口 --- server/routers/auth_router.py | 6 ++++-- src/agents/chatbot/tools.py | 11 +++++++++-- src/storage/minio/__init__.py | 4 ++-- src/storage/minio/client.py | 24 ++--------------------- web/src/components/AgentChatComponent.vue | 2 +- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/server/routers/auth_router.py b/server/routers/auth_router.py index a6835f38..30e248bd 100644 --- a/server/routers/auth_router.py +++ b/server/routers/auth_router.py @@ -1,4 +1,5 @@ import re +import uuid from fastapi import APIRouter, Depends, HTTPException, Request, status, UploadFile, File from fastapi.security import OAuth2PasswordRequestForm @@ -12,7 +13,7 @@ from server.utils.auth_middleware import get_admin_user, get_current_user, get_d from server.utils.auth_utils import AuthUtils from server.utils.user_utils import generate_unique_user_id, validate_username, is_valid_phone_number from server.utils.common_utils import log_operation -from src.storage.minio import upload_image_to_minio +from src.storage.minio import aupload_file_to_minio from src.utils.datetime_utils import utc_now # 创建路由器 @@ -627,7 +628,8 @@ async def upload_user_avatar( file_extension = file.filename.split(".")[-1].lower() if file.filename and "." in file.filename else "jpg" # 上传到MinIO - avatar_url = upload_image_to_minio("avatar", file_content, file_extension) + file_name = f"{uuid.uuid4()}.{file_extension}" + avatar_url = await aupload_file_to_minio("avatar", file_name, file_content, file_extension) # 更新用户头像 current_user.avatar = avatar_url diff --git a/src/agents/chatbot/tools.py b/src/agents/chatbot/tools.py index 4ae144a0..a8f3cca3 100644 --- a/src/agents/chatbot/tools.py +++ b/src/agents/chatbot/tools.py @@ -1,4 +1,5 @@ import os +import uuid from typing import Any import requests @@ -6,7 +7,7 @@ from langchain.tools import tool from src.agents.common import get_buildin_tools from src.agents.common.toolkits.mysql import get_mysql_tools -from src.storage.minio import upload_image_to_minio +from src.storage.minio import aupload_file_to_minio from src.utils import logger @@ -39,7 +40,13 @@ async def text_to_img_qwen(text: str) -> str: response = requests.get(image_url) file_data = response.content - image_url = upload_image_to_minio(bucket_name="generated-images", data=file_data, file_extension="jpg") + file_name = f"{uuid.uuid4()}.jpg" + image_url = await aupload_file_to_minio( + bucket_name="generated-images", + file_name=file_name, + data=file_data, + file_extension="jpg" + ) logger.info(f"Image uploaded. URL: {image_url}") return image_url diff --git a/src/storage/minio/__init__.py b/src/storage/minio/__init__.py index ad7c2dd3..0c8ce9af 100644 --- a/src/storage/minio/__init__.py +++ b/src/storage/minio/__init__.py @@ -4,7 +4,7 @@ MinIO 存储模块 """ # 导出核心功能 -from .client import MinIOClient, StorageError, UploadResult, get_minio_client, upload_image_to_minio +from .client import MinIOClient, StorageError, UploadResult, get_minio_client, aupload_file_to_minio from .utils import generate_unique_filename, get_file_size # 为了向后兼容,导出常用的函数 @@ -12,7 +12,7 @@ __all__ = [ # 核心功能 "MinIOClient", "get_minio_client", - "upload_image_to_minio", + "aupload_file_to_minio", # 异常类 "StorageError", "UploadResult", diff --git a/src/storage/minio/client.py b/src/storage/minio/client.py index 63154161..4630c4be 100644 --- a/src/storage/minio/client.py +++ b/src/storage/minio/client.py @@ -6,7 +6,6 @@ MinIO 存储客户端 import asyncio import json import os -import uuid from datetime import timedelta from io import BytesIO @@ -289,24 +288,6 @@ def get_minio_client() -> MinIOClient: return _default_client -def upload_image_to_minio(bucket_name: str, data: bytes, file_extension: str = "jpg") -> str: - """ - 上传图片到 MinIO(保持向后兼容) - - Args: - data: 图片数据 - file_extension: 文件扩展名 - - Returns: - str: 图片访问 URL - """ - client = get_minio_client() - file_name = f"{uuid.uuid4()}.{file_extension}" - client.upload_file( - bucket_name=bucket_name, object_name=file_name, data=data, content_type=f"image/{file_extension}" - ) - res_url = client.get_presigned_url(bucket_name=bucket_name, object_name=file_name, days=7) - return res_url async def aupload_file_to_minio(bucket_name: str, file_name: str, data: bytes, file_extension: str) -> str: @@ -325,6 +306,5 @@ async def aupload_file_to_minio(bucket_name: str, file_name: str, data: bytes, f # 根据扩展名猜测 content_type content_type = client._guess_content_type(file_extension) # 上传文件 - await client.aupload_file(bucket_name, file_name, data, content_type) - res_url = client.get_presigned_url(bucket_name, file_name, days=7) - return res_url + upload_result = await client.aupload_file(bucket_name, file_name, data, content_type) + return upload_result.url diff --git a/web/src/components/AgentChatComponent.vue b/web/src/components/AgentChatComponent.vue index 0a8c0cd5..0d1e050f 100644 --- a/web/src/components/AgentChatComponent.vue +++ b/web/src/components/AgentChatComponent.vue @@ -338,7 +338,7 @@ const currentAgentName = computed(() => { const agent = agents.value.find(a => a.id === agentId); return agent ? agent.name : '智能体'; } - return '智能体'; + return '智能体加载中……'; }); const currentAgent = computed(() => {