Skip to content

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

fix(upload): decode image pixels before persisting material

Rudy Celeklie15ec5e6a614
diff --git a/app/services/material_upload.py b/app/services/material_upload.py
index 8626df91..da2d7813 100644
--- a/app/services/material_upload.py
+++ b/app/services/material_upload.py
@@ -148,6 +148,11 @@ def _validate_image(file_path: str, extension: str) -> None:
"uploaded image content does not match its file extension"
)
image.verify()
+ # verify() checks structure but may accept truncated JPEG pixel data.
+ # Reopen after verify() (which invalidates the decoder) and decode
+ # every pixel before publishing the upload to later render tasks.
+ with Image.open(file_path) as image:
+ image.load()
except MaterialUploadError:
raise
except (
diff --git a/test/services/test_material_upload.py b/test/services/test_material_upload.py
index efb57f54..953a8dc9 100644
--- a/test/services/test_material_upload.py
+++ b/test/services/test_material_upload.py
@@ -200,6 +200,22 @@ class TestMaterialUploadService(unittest.TestCase):
self.assertTrue(stored_name.endswith(".png"))
self.assertEqual(len(os.listdir(temp_dir)), 1)
+ def test_truncated_jpeg_is_rejected_before_publishing(self):
+ # Pillow's verify() accepts this missing end marker, but decoding the
+ # pixels later raises OSError and would fail an otherwise valid task.
+ truncated_jpeg = _image_bytes("JPEG")[:-2]
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with patch.object(
+ material_upload, "uploaded_material_dir", return_value=temp_dir
+ ):
+ with self.assertRaisesRegex(
+ material_upload.MaterialUploadError, "valid JPEG"
+ ):
+ material_upload.save_material_upload(
+ "truncated.jpg", io.BytesIO(truncated_jpeg)
+ )
+ self.assertEqual(os.listdir(temp_dir), [])
+
def test_webm_and_bmp_materials_are_accepted(self):
with tempfile.TemporaryDirectory() as temp_dir:
with (