fix(cover): 修复预览视频存储路径错误导致封面生成 500 #1357
@@ -137,6 +137,12 @@ def editor_generate_cover(
|
||||
|
||||
storage_svc = get_shared_storage_service()
|
||||
primary_video_url = storage_svc.get_url(rendered_storage_key)
|
||||
# 防御性规范化:合并路径中的双斜杠(// -> /),但保留协议头的 ://
|
||||
# 历史数据中 project_id 为空时会产生 projects//tasks/ 路径,
|
||||
# MediaKit 的 HTTP 客户端会规范化 URL 导致 404
|
||||
if primary_video_url:
|
||||
import re as _re
|
||||
primary_video_url = _re.sub(r'(?<!:)//', '/', primary_video_url)
|
||||
logger.info(
|
||||
"获取预览视频URL用于封面生成: plan_id=%s url=%s",
|
||||
plan_id,
|
||||
|
||||
@@ -223,7 +223,12 @@ def _get_task_output_url(task, gen_task_repo, db) -> str:
|
||||
use_case = ListGeneratedVideosByTaskUseCase(video_repo)
|
||||
videos = use_case.execute(task.id)
|
||||
if videos:
|
||||
return getattr(videos[0], "file_url", "") or ""
|
||||
url = getattr(videos[0], "file_url", "") or ""
|
||||
# 规范化:合并路径中的双斜杠(保留协议头 ://)
|
||||
if url:
|
||||
import re as _re
|
||||
url = _re.sub(r"(?<!:)//", "/", url)
|
||||
return url
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
|
||||
@@ -1262,7 +1262,9 @@ def _upload_and_record(
|
||||
Returns:
|
||||
(file_url, duration, file_size, video_count)
|
||||
"""
|
||||
storage_key = f"generated/projects/{project_id}/tasks/{task_id}/{output_path.name}"
|
||||
# project_id 可能为空(模板编辑器草稿不属于任何项目),过滤空段避免 OSS key 出现 //
|
||||
path_parts = [p for p in ("generated", "projects", project_id, "tasks", task_id, output_path.name) if p]
|
||||
storage_key = "/".join(path_parts)
|
||||
file_size = output_path.stat().st_size
|
||||
|
||||
# 上传 OSS
|
||||
|
||||
@@ -396,6 +396,12 @@ def _call_ai_cover_service(
|
||||
|
||||
# ai_frame / ai_regenerate - 尝试调用 MediaKit
|
||||
if primary_video_url:
|
||||
# 规范化 URL:合并路径中的双斜杠(保留协议头 ://)
|
||||
# 历史数据中 project_id 为空时 OSS key 会出现 projects//tasks/ 路径
|
||||
import re as _re
|
||||
|
||||
primary_video_url = _re.sub(r"(?<!:)//", "/", primary_video_url)
|
||||
|
||||
from packages.shared.mediakit_client import get_mediakit_client
|
||||
|
||||
client = get_mediakit_client()
|
||||
|
||||
@@ -191,6 +191,33 @@ class TestAICoverService:
|
||||
primary_video_url="https://example.com/nonexistent.mp4",
|
||||
)
|
||||
|
||||
@patch("packages.shared.ai_service.http_requests.head")
|
||||
@patch("packages.shared.mediakit_client.get_mediakit_client")
|
||||
def test_call_ai_cover_url_double_slash_normalized(self, mock_get_client, mock_head):
|
||||
"""URL 路径中的双斜杠应被规范化,避免 MediaKit 404."""
|
||||
dirty_url = "https://oss.example.com/generated/projects//tasks/abc123/rendered.mp4"
|
||||
clean_url = "https://oss.example.com/generated/projects/tasks/abc123/rendered.mp4"
|
||||
|
||||
mock_head.return_value.status_code = 200
|
||||
mock_client = Mock()
|
||||
mock_client.is_available = True
|
||||
mock_client.extract_frames.return_value = []
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
from packages.shared.ai_service import _call_ai_cover_service
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
_call_ai_cover_service(
|
||||
plan_id="plan-1",
|
||||
asset_ids=["a1"],
|
||||
cover_type="ai_frame",
|
||||
primary_video_url=dirty_url,
|
||||
)
|
||||
|
||||
# HEAD 请求使用规范化后的 URL
|
||||
mock_head.assert_called_once()
|
||||
assert mock_head.call_args[0][0] == clean_url
|
||||
|
||||
@patch("packages.shared.ai_service.http_requests.head")
|
||||
@patch("packages.shared.mediakit_client.get_mediakit_client")
|
||||
def test_call_ai_cover_with_mediakit_failure_raises(self, mock_get_client, mock_head):
|
||||
|
||||
Reference in New Issue
Block a user