fix: support url-based work downloads
This commit is contained in:
@@ -5,6 +5,7 @@ from sqlalchemy import desc
|
||||
from typing import List
|
||||
import os
|
||||
import mimetypes
|
||||
from urllib.parse import urlparse
|
||||
from app.core.database import get_db
|
||||
from app.models.work import Work
|
||||
from app.models.order import Order, OrderStatus
|
||||
@@ -15,6 +16,23 @@ from app.schemas.work import WorkResponse, WorkListResponse
|
||||
|
||||
router = APIRouter(prefix="/works", tags=["作品"])
|
||||
|
||||
|
||||
def resolve_upload_file_path(image_path: str) -> str:
|
||||
"""兼容 /uploads 相对路径和完整 URL 两种历史存储格式。"""
|
||||
if not image_path:
|
||||
return ""
|
||||
|
||||
normalized = image_path.strip()
|
||||
|
||||
if normalized.startswith(("http://", "https://")):
|
||||
normalized = urlparse(normalized).path or ""
|
||||
|
||||
normalized = normalized.lstrip("/")
|
||||
if normalized.startswith("uploads/"):
|
||||
normalized = normalized[len("uploads/"):]
|
||||
|
||||
return os.path.join(settings.UPLOAD_DIR, normalized)
|
||||
|
||||
@router.get("", response_model=WorkListResponse, summary="获取作品列表")
|
||||
def get_works(
|
||||
page: int = Query(1, ge=1),
|
||||
@@ -139,10 +157,7 @@ def download_work(
|
||||
)
|
||||
|
||||
# 构建原图文件路径
|
||||
relative_path = work.original_image.lstrip("/")
|
||||
if relative_path.startswith("uploads/"):
|
||||
relative_path = relative_path[len("uploads/"):]
|
||||
full_path = os.path.join(settings.UPLOAD_DIR, relative_path)
|
||||
full_path = resolve_upload_file_path(work.original_image)
|
||||
|
||||
# 检查文件是否存在
|
||||
if not os.path.exists(full_path):
|
||||
|
||||
Reference in New Issue
Block a user