Skip to content

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

fix(api): stream empty artifacts without a Range header

Rudy Celekli4458f2a092e2
diff --git a/app/controllers/v1/video.py b/app/controllers/v1/video.py
index 989c218e..dc7763ec 100644
--- a/app/controllers/v1/video.py
+++ b/app/controllers/v1/video.py
@@ -159,6 +159,11 @@ def _parse_byte_range(
range_header: str | None, file_size: int, request_id: str
) -> tuple[int, int]:
"""解析单段 HTTP Range,并把无效或越界请求稳定转换成 416。"""
+ # 416 只用于回应 Range 请求。没有 Range 时空文件也是完整表示,
+ # 返回 0 字节的 200,与 /download 的行为一致。
+ if not range_header:
+ return 0, file_size - 1
+
if file_size <= 0:
raise HttpException(
task_id=request_id,
@@ -166,9 +171,6 @@ def _parse_byte_range(
message=f"{request_id}: requested range is not satisfiable",
)
- if not range_header:
- return 0, file_size - 1
-
try:
# 视频播放器这里只需要单段 bytes range。拒绝多段请求可以避免返回体
# 与 Content-Range 不一致,也避免异常字符串落入 int() 产生 500。
diff --git a/test/services/test_stream_empty_artifact.py b/test/services/test_stream_empty_artifact.py
new file mode 100644
index 00000000..347b6b2c
--- /dev/null
+++ b/test/services/test_stream_empty_artifact.py
@@ -0,0 +1,38 @@
+from unittest.mock import patch
+
+from fastapi.testclient import TestClient
+
+from app import asgi
+from app.config import config
+from app.controllers.v1 import video as controller
+
+
+def _get(tmp_path, headers=None):
+ with (
+ patch.dict(config.app, {"api_key": ""}),
+ patch.object(controller.utils, "task_dir", return_value=str(tmp_path)),
+ ):
+ return TestClient(asgi.get_application()).get(
+ "/api/v1/stream/empty.srt", headers=headers or {}
+ )
+
+
+def test_stream_returns_empty_body_for_empty_file_without_range(tmp_path):
+ # 416 only answers a Range request; a plain GET of an empty artifact is a
+ # complete zero-length representation, matching /download.
+ (tmp_path / "empty.srt").write_bytes(b"")
+
+ response = _get(tmp_path)
+
+ assert response.status_code == 200
+ assert response.content == b""
+ assert response.headers["content-length"] == "0"
+ assert "content-range" not in response.headers
+
+
+def test_stream_still_rejects_range_on_empty_file(tmp_path):
+ (tmp_path / "empty.srt").write_bytes(b"")
+
+ response = _get(tmp_path, {"Range": "bytes=0-"})
+
+ assert response.status_code == 416