Skip to content

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

fix(api): return complete streams for unverifiable If-Range

Rudy Celeklia99c6dc1bbcf
diff --git a/app/controllers/v1/video.py b/app/controllers/v1/video.py
index 6a7f7391..989c218e 100644
--- a/app/controllers/v1/video.py
+++ b/app/controllers/v1/video.py
@@ -512,6 +512,11 @@ async def stream_video(request: Request, file_path: str):
tasks_dir = utils.task_dir()
video_path = _resolve_path_within_directory(tasks_dir, file_path, request_id)
range_header = request.headers.get("Range")
+ if range_header and request.headers.get("If-Range") is not None:
+ # This stream does not publish strong ETag/Last-Modified validators.
+ # An unverifiable If-Range must fall back to the full representation,
+ # so a client cannot combine old cached bytes with a new video tail.
+ range_header = None
# The body is produced after this handler returns. Open now so a task
# deletion or replacement between headers and iteration cannot make the
# stream fail or mismatch the size used for Content-Range.
diff --git a/test/services/test_stream_unverifiable_if_range.py b/test/services/test_stream_unverifiable_if_range.py
new file mode 100644
index 00000000..20279e02
--- /dev/null
+++ b/test/services/test_stream_unverifiable_if_range.py
@@ -0,0 +1,30 @@
+from unittest.mock import patch
+
+import pytest
+from fastapi.testclient import TestClient
+
+from app import asgi
+from app.config import config
+from app.controllers.v1 import video as controller
+
+
[email protected]('validator', ['"older-video-revision"', 'Tue, 01 Jan 2019 00:00:00 GMT'])
+def test_stream_returns_complete_video_for_unverifiable_if_range(tmp_path, validator):
+ payload = b'complete current video fixture'
+ (tmp_path / 'clip.mp4').write_bytes(payload)
+ with patch.dict(config.app, {'api_key': ''}), patch.object(controller.utils, 'task_dir', return_value=str(tmp_path)):
+ response = TestClient(asgi.get_application()).get('/api/v1/stream/clip.mp4', headers={
+ 'Range': 'bytes=9-', 'If-Range': validator,
+ })
+ assert response.status_code == 200
+ assert response.content == payload
+ assert 'content-range' not in response.headers
+ assert response.headers['content-length'] == str(len(payload))
+
+
+def test_stream_keeps_unconditional_partial_request(tmp_path):
+ (tmp_path / 'clip.mp4').write_bytes(b'0123456789')
+ with patch.dict(config.app, {'api_key': ''}), patch.object(controller.utils, 'task_dir', return_value=str(tmp_path)):
+ response = TestClient(asgi.get_application()).get('/api/v1/stream/clip.mp4', headers={'Range': 'bytes=3-'})
+ assert response.status_code == 206
+ assert response.content == b'3456789'