1、图片生成同步接口超时风险
2、图片返回的 URL 是本地路径 3、视频生成中媒体文件重复下载 4、幂等性检查无数据库唯一约束 5、幂等键冲突返回 409 改为返回已有任务信息 6、虚拟素材库配额校验 TOCTOU 7、项目级联删除与独立素材删除任务并发冲突
This commit is contained in:
@@ -28,17 +28,17 @@ async def create_video_task(
|
||||
idempotency_key: str | None = None,
|
||||
local_media_refs: list[dict] | None = None,
|
||||
) -> ApiGenerationTask:
|
||||
"""创建视频生成任务记录。"""
|
||||
"""创建视频生成任务记录。
|
||||
|
||||
如果调用方已下载好媒体文件(local_media_refs),则直接复用,避免重复下载。
|
||||
"""
|
||||
# 提取文本提示词
|
||||
text_parts = [p.get("text", "") for p in content if p.get("type") == "text"]
|
||||
original_prompt = " ".join(text_parts) if text_parts else content[0].get("text", "") if content else ""
|
||||
|
||||
# 构建 media_references(扁平格式,便于外部读取)
|
||||
# 构建 local_media_json(嵌套格式,与 Volcano SDK 兼容)
|
||||
from app.services.api_v3.file_service import process_media_url
|
||||
|
||||
media_refs = [] # 扁平格式: {"type": "image", "url": "...", "role": "..."}
|
||||
local_media_refs = [] # 本地下载路径(嵌套格式)
|
||||
|
||||
for p in content:
|
||||
ptype = p.get("type", "")
|
||||
@@ -62,19 +62,37 @@ async def create_video_task(
|
||||
"role": p.get("role"),
|
||||
})
|
||||
|
||||
# 下载文件到本地
|
||||
try:
|
||||
local_path = await process_media_url(original_url, media_type)
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to download media %s: %s", original_url[:80], exc)
|
||||
local_path = original_url
|
||||
# 如果调用方已传入 local_media_refs(已下载),直接使用,不再重复下载
|
||||
if local_media_refs is None:
|
||||
from app.services.api_v3.file_service import process_media_url
|
||||
|
||||
# 本地路径使用嵌套格式(与 Volcano SDK 兼容)
|
||||
local_media_refs.append({
|
||||
"type": ptype,
|
||||
ptype: {"url": local_path},
|
||||
"role": p.get("role"),
|
||||
})
|
||||
local_media_refs = [] # 本地下载路径(嵌套格式)
|
||||
for p in content:
|
||||
ptype = p.get("type", "")
|
||||
if ptype == "text":
|
||||
continue
|
||||
|
||||
original_url = ""
|
||||
if ptype == "image_url" and p.get("image_url"):
|
||||
original_url = p["image_url"].get("url", "")
|
||||
elif ptype == "video_url" and p.get("video_url"):
|
||||
original_url = p["video_url"].get("url", "")
|
||||
elif ptype == "audio_url" and p.get("audio_url"):
|
||||
original_url = p["audio_url"].get("url", "")
|
||||
|
||||
# 下载文件到本地
|
||||
try:
|
||||
local_path = await process_media_url(original_url, ptype.replace("_url", ""))
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to download media %s: %s", original_url[:80], exc)
|
||||
local_path = original_url
|
||||
|
||||
# 本地路径使用嵌套格式(与 Volcano SDK 兼容)
|
||||
local_media_refs.append({
|
||||
"type": ptype,
|
||||
ptype: {"url": local_path},
|
||||
"role": p.get("role"),
|
||||
})
|
||||
|
||||
media_references_json = json.dumps(media_refs, ensure_ascii=False) if media_refs else None
|
||||
local_media_json = json.dumps(local_media_refs, ensure_ascii=False) if local_media_refs else None
|
||||
|
||||
Reference in New Issue
Block a user