Skip to content

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

fix(storage): make first-use directory creation concurrency safe

Rudy Celekli27f9bdc5a868
diff --git a/app/utils/utils.py b/app/utils/utils.py
index 571522bb..7a672760 100644
--- a/app/utils/utils.py
+++ b/app/utils/utils.py
@@ -94,8 +94,8 @@ def storage_dir(sub_dir: str = "", create: bool = False):
d = os.path.join(root_dir(), "storage")
if sub_dir:
d = os.path.join(d, sub_dir)
- if create and not os.path.exists(d):
- os.makedirs(d)
+ if create:
+ os.makedirs(d, exist_ok=True)
return d
@@ -111,8 +111,7 @@ def task_dir(sub_dir: str = ""):
d = os.path.join(storage_dir(), "tasks")
if sub_dir:
d = os.path.join(d, sub_dir)
- if not os.path.exists(d):
- os.makedirs(d)
+ os.makedirs(d, exist_ok=True)
return d
@@ -120,8 +119,7 @@ def font_dir(sub_dir: str = ""):
d = resource_dir("fonts")
if sub_dir:
d = os.path.join(d, sub_dir)
- if not os.path.exists(d):
- os.makedirs(d)
+ os.makedirs(d, exist_ok=True)
return d
@@ -129,8 +127,7 @@ def song_dir(sub_dir: str = ""):
d = resource_dir("songs")
if sub_dir:
d = os.path.join(d, sub_dir)
- if not os.path.exists(d):
- os.makedirs(d)
+ os.makedirs(d, exist_ok=True)
return d
@@ -138,8 +135,7 @@ def public_dir(sub_dir: str = ""):
d = resource_dir("public")
if sub_dir:
d = os.path.join(d, sub_dir)
- if not os.path.exists(d):
- os.makedirs(d)
+ os.makedirs(d, exist_ok=True)
return d
diff --git a/test/services/test_directory_creation.py b/test/services/test_directory_creation.py
new file mode 100644
index 00000000..7d97cadb
--- /dev/null
+++ b/test/services/test_directory_creation.py
@@ -0,0 +1,56 @@
+from concurrent.futures import ThreadPoolExecutor
+from pathlib import Path
+from tempfile import TemporaryDirectory
+from threading import Barrier
+from unittest.mock import patch
+
+import pytest
+
+from app.utils import utils
+
+
[email protected]('helper', ['storage', 'task', 'font', 'song', 'public'])
+def test_directory_helpers_allow_concurrent_first_use(helper):
+ with TemporaryDirectory() as temp_dir:
+ root = Path(temp_dir)
+ (root / 'storage').mkdir()
+ (root / 'storage' / 'tasks').mkdir()
+ for name in ('fonts', 'songs', 'public'):
+ (root / 'resource' / name).mkdir(parents=True)
+ make_directory = utils.os.makedirs
+ barrier = Barrier(2)
+
+ def concurrent_makedirs(path, *args, **kwargs):
+ barrier.wait(timeout=5)
+ return make_directory(path, *args, **kwargs)
+
+ def create():
+ if helper == 'storage':
+ return utils.storage_dir('first-use', create=True)
+ return getattr(utils, f'{helper}_dir')('first-use')
+
+ with (
+ patch.object(utils, 'root_dir', return_value=temp_dir),
+ patch.object(utils.os, 'makedirs', side_effect=concurrent_makedirs),
+ ThreadPoolExecutor(max_workers=2) as executor,
+ ):
+ futures = [executor.submit(create) for _ in range(2)]
+ paths = [future.result(timeout=10) for future in futures]
+ assert paths[0] == paths[1]
+ assert Path(paths[0]).is_dir()
+
+
+def test_storage_lookup_does_not_create_directory():
+ with TemporaryDirectory() as root, patch.object(utils, 'root_dir', return_value=root):
+ result = utils.storage_dir('lookup-only')
+ assert not Path(result).exists()
+
+
+def test_directory_creation_does_not_hide_file_collision():
+ with TemporaryDirectory() as root, patch.object(utils, 'root_dir', return_value=root):
+ storage = Path(root, 'storage')
+ storage.mkdir()
+ (storage / 'collision').write_text('keep', encoding='utf-8')
+ with pytest.raises(FileExistsError):
+ utils.storage_dir('collision', create=True)
+ assert (storage / 'collision').read_text() == 'keep'