Skip to content

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

fix(api): apply the same Windows filename rules to local material uploads

c0206272ee81cfe41c7
diff --git a/app/services/material_upload.py b/app/services/material_upload.py
index ce323d36..2bad18f5 100644
--- a/app/services/material_upload.py
+++ b/app/services/material_upload.py
@@ -31,6 +31,12 @@ SUPPORTED_MATERIAL_EXTENSIONS = (
_COPY_CHUNK_BYTES = 1024 * 1024
_INTERNAL_UPLOAD_PREFIX = ".material-upload-"
+_WINDOWS_INVALID_FILENAME_CHARS = frozenset('<>:"|?*')
+_WINDOWS_RESERVED_FILENAMES = frozenset(
+ {"CON", "PRN", "AUX", "NUL"}
+ | {f"COM{index}" for index in range(1, 10)}
+ | {f"LPT{index}" for index in range(1, 10)}
+)
_IMAGE_FORMATS_BY_EXTENSION = {
".jpg": frozenset({"JPEG"}),
".jpeg": frozenset({"JPEG"}),
@@ -86,9 +92,19 @@ def sanitize_material_filename(filename: str) -> str:
or safe_name in {".", ".."}
or len(safe_name) > 255
or any(ord(character) < 32 for character in safe_name)
+ or any(character in _WINDOWS_INVALID_FILENAME_CHARS for character in safe_name)
or safe_name.lower().startswith(_INTERNAL_UPLOAD_PREFIX)
):
raise MaterialUploadError("invalid local material filename")
+
+ # Keep the same rule as bgm.sanitize_upload_filename: Windows resolves the
+ # segment before the extension as a device name, so CON.mp4 and LPT1.webm
+ # cannot be created as ordinary files there. Even though the stored name is
+ # a UUID in both endpoints, rejecting these names up front keeps the two
+ # upload APIs behaving identically on every platform.
+ windows_basename = safe_name.split(".", 1)[0].rstrip(" .").upper()
+ if windows_basename in _WINDOWS_RESERVED_FILENAMES:
+ raise MaterialUploadError("invalid local material filename")
_material_kind(safe_name)
return safe_name
diff --git a/test/services/test_material_upload.py b/test/services/test_material_upload.py
index cede5df6..67e13371 100644
--- a/test/services/test_material_upload.py
+++ b/test/services/test_material_upload.py
@@ -45,6 +45,37 @@ class TestMaterialUploadService(unittest.TestCase):
with self.assertRaises(material_upload.MaterialUploadError):
material_upload.sanitize_material_filename(filename)
+ def test_sanitize_filename_rejects_windows_invalid_and_reserved_names(self):
+ # bgm.sanitize_upload_filename already applies these Windows rules to the
+ # background-music upload. The local material upload also stores its file
+ # under a UUID, so the two endpoints must agree on which client-supplied
+ # names they accept instead of diverging by platform.
+ for filename in (
+ "CON.mp4",
+ "con.mp4",
+ "lpt1.webm",
+ "aux.extra.mp4",
+ "bad:name.mp4",
+ "bad?.mp4",
+ "bad<1>.mp4",
+ 'quote".mp4',
+ "pipe|name.mp4",
+ "star*.mp4",
+ ):
+ with self.subTest(filename=filename):
+ with self.assertRaises(material_upload.MaterialUploadError):
+ material_upload.sanitize_material_filename(filename)
+
+ # Names that merely contain a reserved word, or an invalid character in
+ # the extension-less stem, are still ordinary uploads.
+ self.assertEqual(
+ material_upload.sanitize_material_filename("console.mp4"), "console.mp4"
+ )
+ self.assertEqual(
+ material_upload.sanitize_material_filename(r"C:\videos\aux-extra.png"),
+ "aux-extra.png",
+ )
+
def test_video_upload_is_chunked_validated_and_atomically_persisted(self):
with tempfile.TemporaryDirectory() as temp_dir:
source = io.BytesIO(b"decodable-video-placeholder")