Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(api): return download URLs for generated task outputs

Rudy Celekli7496aaae9160
diff --git a/app/controllers/v1/video.py b/app/controllers/v1/video.py
index d75c1674..4617b96c 100644
--- a/app/controllers/v1/video.py
+++ b/app/controllers/v1/video.py
@@ -136,6 +136,22 @@ def _task_file_to_uri(file: str, endpoint: str, task_dir: str, request_id: str)
return f"/{uri_path}"
+def _task_response_data(task: dict, endpoint: str, task_dir: str, request_id: str) -> dict:
+ response_task = _public_task_data(task)
+ for key in ("videos", "combined_videos"):
+ if key in task:
+ response_task[key] = [
+ _task_file_to_uri(file, endpoint, task_dir, request_id)
+ for file in task[key]
+ ]
+ for key in ("audio_file", "subtitle_path"):
+ if task.get(key):
+ response_task[key] = _task_file_to_uri(
+ task[key], endpoint, task_dir, request_id
+ )
+ return response_task
+
+
def _parse_byte_range(
range_header: str | None, file_size: int, request_id: str
) -> tuple[int, int]:
@@ -261,9 +277,14 @@ def get_all_tasks(
page_size: int = Query(10, ge=1),
):
tasks, total = sm.state.get_all_tasks(page, page_size)
+ request_id = base.get_task_id(request)
+ endpoint = config.app.get("endpoint", "").rstrip("/")
+ task_dir = utils.task_dir()
response = {
- "tasks": [_public_task_data(task) for task in tasks],
+ "tasks": [
+ _task_response_data(task, endpoint, task_dir, request_id) for task in tasks
+ ],
"total": total,
"page": page,
"page_size": page_size,
@@ -285,18 +306,7 @@ def get_task(
task = sm.state.get_task(task_id)
if task:
task_dir = utils.task_dir()
- response_task = _public_task_data(task)
-
- if "videos" in task:
- response_task["videos"] = [
- _task_file_to_uri(v, endpoint, task_dir, request_id)
- for v in task["videos"]
- ]
- if "combined_videos" in task:
- response_task["combined_videos"] = [
- _task_file_to_uri(v, endpoint, task_dir, request_id)
- for v in task["combined_videos"]
- ]
+ response_task = _task_response_data(task, endpoint, task_dir, request_id)
return utils.get_response(200, response_task)
raise HttpException(
diff --git a/test/services/test_controller_video.py b/test/services/test_controller_video.py
index c34a27dc..f3ee2816 100644
--- a/test/services/test_controller_video.py
+++ b/test/services/test_controller_video.py
@@ -311,6 +311,49 @@ class TestVideoControllerTasks(unittest.TestCase):
)
get_all.assert_called_once_with(2, 10)
+ def test_task_list_returns_download_urls_without_mutating_state(self):
+ """List and detail endpoints must expose the same usable video URLs."""
+ task_id = "listed-task-url"
+ task_dir = utils.task_dir(task_id)
+ video_path = os.path.join(task_dir, "final-1.mp4")
+ audio_path = os.path.join(task_dir, "audio.mp3")
+ subtitle_path = os.path.join(task_dir, "subtitle.srt")
+ Path(video_path).write_bytes(b"fake-video")
+ Path(audio_path).write_bytes(b"fake-audio")
+ Path(subtitle_path).write_text("subtitle", encoding="utf-8")
+ stored_task = {
+ "task_id": task_id,
+ "state": const.TASK_STATE_COMPLETE,
+ "videos": [video_path],
+ "combined_videos": [video_path],
+ "audio_file": audio_path,
+ "subtitle_path": subtitle_path,
+ }
+
+ try:
+ with (
+ patch.object(
+ video_controller.sm.state,
+ "get_all_tasks",
+ return_value=([stored_task], 1),
+ ),
+ patch.dict(config.app, {"endpoint": ""}),
+ ):
+ response = video_controller.get_all_tasks(
+ self._request(), page=1, page_size=10
+ )
+
+ listed = response["data"]["tasks"][0]
+ expected_url = f"/tasks/{task_id}/final-1.mp4"
+ self.assertEqual(listed["videos"], [expected_url])
+ self.assertEqual(listed["combined_videos"], [expected_url])
+ self.assertEqual(listed["audio_file"], f"/tasks/{task_id}/audio.mp3")
+ self.assertEqual(listed["subtitle_path"], f"/tasks/{task_id}/subtitle.srt")
+ self.assertEqual(stored_task["videos"], [video_path])
+ self.assertEqual(stored_task["audio_file"], audio_path)
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
def test_task_query_returns_relative_url_without_mutating_state(self):
"""
endpoint 未配置时应返回相对任务 URL,且不能把展示用 URL 回写到状态,
@@ -319,7 +362,11 @@ class TestVideoControllerTasks(unittest.TestCase):
task_id = "controller-task-url"
task_dir = utils.task_dir(task_id)
video_path = os.path.join(task_dir, "final-1.mp4")
+ audio_path = os.path.join(task_dir, "audio.mp3")
+ subtitle_path = os.path.join(task_dir, "subtitle.srt")
Path(video_path).write_bytes(b"fake-video")
+ Path(audio_path).write_bytes(b"fake-audio")
+ Path(subtitle_path).write_text("subtitle", encoding="utf-8")
try:
sm.state.update_task(
@@ -327,6 +374,8 @@ class TestVideoControllerTasks(unittest.TestCase):
state=const.TASK_STATE_COMPLETE,
videos=[video_path],
combined_videos=[video_path],
+ audio_file=audio_path,
+ subtitle_path=subtitle_path,
cross_post_owner="localhost:123:internal",
)
with patch.dict(config.app, {"endpoint": ""}):
@@ -338,6 +387,8 @@ class TestVideoControllerTasks(unittest.TestCase):
response["data"]["videos"],
[f"/tasks/{task_id}/final-1.mp4"],
)
+ self.assertEqual(response["data"]["audio_file"], f"/tasks/{task_id}/audio.mp3")
+ self.assertEqual(response["data"]["subtitle_path"], f"/tasks/{task_id}/subtitle.srt")
self.assertNotIn("cross_post_owner", response["data"])
self.assertIn("cross_post_owner", sm.state.get_task(task_id))
self.assertEqual(sm.state.get_task(task_id)["videos"], [video_path])