refactor(storage): 将图片上传功能重构为异步文件上传
重构图片上传功能为通用的异步文件上传,支持自定义文件名 移除同步上传方法,统一使用异步接口
This commit is contained in:
parent
eb9fd5b798
commit
6003563adb
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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(() => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user