Skip to content

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

fix(material): tolerate concurrent first-use download directories

Rudy Celeklid6d3e6d3cf13
diff --git a/app/services/material.py b/app/services/material.py
index 5cb1b049..97e31a27 100644
--- a/app/services/material.py
+++ b/app/services/material.py
@@ -1137,8 +1137,7 @@ def save_video(video_url: str, save_dir: str = "") -> str:
if not save_dir:
save_dir = utils.storage_dir("cache_videos")
- if not os.path.exists(save_dir):
- os.makedirs(save_dir)
+ os.makedirs(save_dir, exist_ok=True)
# Query parameters can identify the asset itself (for example,
# /download?file_id=123). Dropping the query makes unrelated paid videos
diff --git a/test/services/test_download_directory_race.py b/test/services/test_download_directory_race.py
new file mode 100644
index 00000000..a727ca6d
--- /dev/null
+++ b/test/services/test_download_directory_race.py
@@ -0,0 +1,29 @@
+from concurrent.futures import ThreadPoolExecutor
+from pathlib import Path
+import threading
+from unittest.mock import MagicMock, patch
+from app.services import material
+
+def test_parallel_first_downloads_share_new_directory(tmp_path):
+ destination = tmp_path / "cache"
+ barrier = threading.Barrier(2)
+ real_makedirs = material.os.makedirs
+ def makedirs(name, **kwargs):
+ if str(name) == str(destination):
+ barrier.wait(timeout=3)
+ return real_makedirs(name, **kwargs)
+ response = MagicMock()
+ response.__enter__.return_value = response
+ response.headers = {}
+ response.iter_content.side_effect = lambda **kwargs: iter([b"video fixture"])
+ def decode(name):
+ assert Path(name).read_bytes() == b"video fixture"
+ return MagicMock(duration=3.0, fps=30)
+ with (patch.object(material.os, "makedirs", side_effect=makedirs),
+ patch.object(material.requests, "get", return_value=response),
+ patch.object(material, "VideoFileClip", side_effect=decode)):
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ results = list(pool.map(lambda n: material.save_video(f"https://example.test/{n}.mp4", str(destination)), [1, 2]))
+ assert len(set(results)) == 2
+ assert all(Path(path).read_bytes() == b"video fixture" for path in results)
+ assert len(list(destination.iterdir())) == 2