From 358241d115df7bad06032977f7e3b6ffafadd7f1 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Fri, 11 Apr 2025 19:25:11 +0800 Subject: [PATCH] update --- app/api/v1/tryon.py | 31 +++++++++++++++---------------- app/services/cos.py | 5 ++++- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/api/v1/tryon.py b/app/api/v1/tryon.py index b39aa3f..16eb9f9 100644 --- a/app/api/v1/tryon.py +++ b/app/api/v1/tryon.py @@ -17,7 +17,6 @@ from app.schemas.tryon import TryonHistoryModel from app.core.exceptions import BusinessError from app.services import cos as cos_service from sqlalchemy import select -import httpx logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) @@ -127,21 +126,21 @@ async def check_tryon_status( completion = await dashscope_service.check_tryon_status(tryon_history.task_id) if completion.get("status") == "SUCCEEDED": completion_url = completion.get("image_url") + print(completion_url) - # 上传到腾讯云COS - async with httpx.AsyncClient() as client: - response = await client.get(completion_url) - file_content = response.content + url = await cos_service.upload_file_from_url(completion_url) + tryon_history.status = TryonStatus.COMPLETED + tryon_history.completion_url = url + await db.commit() - url = await cos_service.upload_file(file_content, "jpg") - tryon_history.status = TryonStatus.COMPLETED - tryon_history.completion_url = url - await db.commit() + return StandardResponse(code=200, message="检查试穿状态成功", data={ + "history_id": tryon_history.id, + "status": tryon_history.status, + "completion_url": url + }) else: - url = tryon_history.completion_url - - return StandardResponse(code=200, message="检查试穿状态成功", data={ - "history_id": tryon_history.id, - "status": tryon_history.status, - "completion_url": url - }) + return StandardResponse(code=200, message="检查试穿状态成功", data={ + "history_id": tryon_history.id, + "status": tryon_history.status, + "completion_url": tryon_history.completion_url + }) diff --git a/app/services/cos.py b/app/services/cos.py index 4afe3d3..4b8a0e5 100644 --- a/app/services/cos.py +++ b/app/services/cos.py @@ -53,8 +53,11 @@ async def upload_file_from_url(url: str, directory: str = "uploads") -> str: response = requests.get(url, timeout=10) response.raise_for_status() - # 获取文件扩展名 + # 获取文件扩展名, 要考虑有?的情况 file_extension = os.path.splitext(url)[1] + if "?" in file_extension: + file_extension = file_extension.split("?")[0] + logger.info(f"文件扩展名: {file_extension}") if not file_extension: # 如果URL没有扩展名,根据内容类型判断 content_type = response.headers.get("Content-Type", "")