- 在valuation模型中新增多个评估字段,包括稀缺等级、市场活动时间等 - 完善用户端输出模型,确保所有字段正确序列化 - 修复文件上传返回URL缺少BASE_URL的问题 - 更新Docker镜像版本至v1.2 - 添加静态文件路径到中间件排除列表 - 优化估值评估创建接口,自动关联当前用户ID
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import os
|
|
from pathlib import Path
|
|
from typing import List
|
|
from fastapi import UploadFile
|
|
from app.schemas.upload import ImageUploadResponse
|
|
from app.settings.config import settings
|
|
|
|
class UploadController:
|
|
"""文件上传控制器"""
|
|
|
|
@staticmethod
|
|
async def upload_image(file: UploadFile) -> ImageUploadResponse:
|
|
"""
|
|
上传图片
|
|
:param file: 上传的图片文件
|
|
:return: 图片URL和文件名
|
|
"""
|
|
# 检查文件类型
|
|
if not file.content_type.startswith('image/'):
|
|
raise ValueError("只支持上传图片文件")
|
|
|
|
# 获取项目根目录
|
|
base_dir = Path(__file__).resolve().parent.parent
|
|
# 图片保存目录
|
|
upload_dir = base_dir / "static" / "images"
|
|
|
|
# 确保目录存在
|
|
if not upload_dir.exists():
|
|
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 生成文件名
|
|
filename = file.filename
|
|
file_path = upload_dir / filename
|
|
|
|
# 如果文件已存在,重命名
|
|
counter = 1
|
|
while file_path.exists():
|
|
name, ext = os.path.splitext(filename)
|
|
filename = f"{name}_{counter}{ext}"
|
|
file_path = upload_dir / filename
|
|
counter += 1
|
|
|
|
# 保存文件
|
|
content = await file.read()
|
|
with open(file_path, "wb") as f:
|
|
f.write(content)
|
|
|
|
# 返回完整的可访问URL
|
|
return ImageUploadResponse(
|
|
url=f"{settings.BASE_URL}/static/images/{filename}",
|
|
filename=filename
|
|
) |