Skip to content

Commit 6003563

Browse files
committed
refactor(storage): 将图片上传功能重构为异步文件上传
重构图片上传功能为通用的异步文件上传,支持自定义文件名 移除同步上传方法,统一使用异步接口
1 parent eb9fd5b commit 6003563

File tree

5 files changed

+18
-29
lines changed

5 files changed

+18
-29
lines changed

server/routers/auth_router.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import uuid
23

34
from fastapi import APIRouter, Depends, HTTPException, Request, status, UploadFile, File
45
from fastapi.security import OAuth2PasswordRequestForm
@@ -12,7 +13,7 @@
1213
from server.utils.auth_utils import AuthUtils
1314
from server.utils.user_utils import generate_unique_user_id, validate_username, is_valid_phone_number
1415
from server.utils.common_utils import log_operation
15-
from src.storage.minio import upload_image_to_minio
16+
from src.storage.minio import aupload_file_to_minio
1617
from src.utils.datetime_utils import utc_now
1718

1819
# 创建路由器
@@ -627,7 +628,8 @@ async def upload_user_avatar(
627628
file_extension = file.filename.split(".")[-1].lower() if file.filename and "." in file.filename else "jpg"
628629

629630
# 上传到MinIO
630-
avatar_url = upload_image_to_minio("avatar", file_content, file_extension)
631+
file_name = f"{uuid.uuid4()}.{file_extension}"
632+
avatar_url = await aupload_file_to_minio("avatar", file_name, file_content, file_extension)
631633

632634
# 更新用户头像
633635
current_user.avatar = avatar_url

src/agents/chatbot/tools.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
2+
import uuid
23
from typing import Any
34

45
import requests
56
from langchain.tools import tool
67

78
from src.agents.common import get_buildin_tools
89
from src.agents.common.toolkits.mysql import get_mysql_tools
9-
from src.storage.minio import upload_image_to_minio
10+
from src.storage.minio import aupload_file_to_minio
1011
from src.utils import logger
1112

1213

@@ -39,7 +40,13 @@ async def text_to_img_qwen(text: str) -> str:
3940
response = requests.get(image_url)
4041
file_data = response.content
4142

42-
image_url = upload_image_to_minio(bucket_name="generated-images", data=file_data, file_extension="jpg")
43+
file_name = f"{uuid.uuid4()}.jpg"
44+
image_url = await aupload_file_to_minio(
45+
bucket_name="generated-images",
46+
file_name=file_name,
47+
data=file_data,
48+
file_extension="jpg"
49+
)
4350
logger.info(f"Image uploaded. URL: {image_url}")
4451
return image_url
4552

src/storage/minio/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"""
55

66
# 导出核心功能
7-
from .client import MinIOClient, StorageError, UploadResult, get_minio_client, upload_image_to_minio
7+
from .client import MinIOClient, StorageError, UploadResult, get_minio_client, aupload_file_to_minio
88
from .utils import generate_unique_filename, get_file_size
99

1010
# 为了向后兼容,导出常用的函数
1111
__all__ = [
1212
# 核心功能
1313
"MinIOClient",
1414
"get_minio_client",
15-
"upload_image_to_minio",
15+
"aupload_file_to_minio",
1616
# 异常类
1717
"StorageError",
1818
"UploadResult",

src/storage/minio/client.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import asyncio
77
import json
88
import os
9-
import uuid
109
from datetime import timedelta
1110
from io import BytesIO
1211

@@ -289,24 +288,6 @@ def get_minio_client() -> MinIOClient:
289288
return _default_client
290289

291290

292-
def upload_image_to_minio(bucket_name: str, data: bytes, file_extension: str = "jpg") -> str:
293-
"""
294-
上传图片到 MinIO(保持向后兼容)
295-
296-
Args:
297-
data: 图片数据
298-
file_extension: 文件扩展名
299-
300-
Returns:
301-
str: 图片访问 URL
302-
"""
303-
client = get_minio_client()
304-
file_name = f"{uuid.uuid4()}.{file_extension}"
305-
client.upload_file(
306-
bucket_name=bucket_name, object_name=file_name, data=data, content_type=f"image/{file_extension}"
307-
)
308-
res_url = client.get_presigned_url(bucket_name=bucket_name, object_name=file_name, days=7)
309-
return res_url
310291

311292

312293
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
325306
# 根据扩展名猜测 content_type
326307
content_type = client._guess_content_type(file_extension)
327308
# 上传文件
328-
await client.aupload_file(bucket_name, file_name, data, content_type)
329-
res_url = client.get_presigned_url(bucket_name, file_name, days=7)
330-
return res_url
309+
upload_result = await client.aupload_file(bucket_name, file_name, data, content_type)
310+
return upload_result.url

web/src/components/AgentChatComponent.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ const currentAgentName = computed(() => {
338338
const agent = agents.value.find(a => a.id === agentId);
339339
return agent ? agent.name : '智能体';
340340
}
341-
return '智能体';
341+
return '智能体加载中……';
342342
});
343343
344344
const currentAgent = computed(() => {

0 commit comments

Comments
 (0)