From 8b73704627a6503d407d035f0cabbd7c037b5c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20St=C3=A4hli?= Date: Tue, 7 Nov 2023 00:50:08 +0100 Subject: [PATCH] catch file not fonund and corrupt images --- filer/management/commands/filer_check.py | 21 +++++++++++++------ tests/test_filer_check.py | 26 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/filer/management/commands/filer_check.py b/filer/management/commands/filer_check.py index d12322eb7..a8010ca32 100644 --- a/filer/management/commands/filer_check.py +++ b/filer/management/commands/filer_check.py @@ -3,6 +3,7 @@ from django.core.files.storage import DefaultStorage from django.core.management.base import BaseCommand from django.utils.module_loading import import_string +from PIL import UnidentifiedImageError from filer import settings as filer_settings @@ -135,16 +136,24 @@ def image_dimensions(self, options): ) self.stdout.write(f"trying to set dimensions on {no_dimensions.count()} files") for image in no_dimensions: + imgfile = None + if image.file_ptr: + file_holder = image.file_ptr + else: + file_holder = image try: imgfile = image.file.file - except ValueError: - imgfile = image.file_ptr.file - imgfile.seek(0) + imgfile.seek(0) + except (FileNotFoundError): + continue if image.file.name.endswith('.svg'): image._width, image._height = VILImage.load(imgfile).size else: - with PILImage.open(imgfile) as pil_image: - image._width, image._height = pil_image.size - image._transparent = easy_thumbnails.utils.is_transparent(pil_image) + try: + with PILImage.open(imgfile) as pil_image: + image._width, image._height = pil_image.size + image._transparent = easy_thumbnails.utils.is_transparent(pil_image) + except UnidentifiedImageError: + continue image.save() return diff --git a/tests/test_filer_check.py b/tests/test_filer_check.py index 45fd18adf..545db99f2 100644 --- a/tests/test_filer_check.py +++ b/tests/test_filer_check.py @@ -70,6 +70,32 @@ def test_delete_orphans(self): call_command('filer_check', delete_orphans=True, interactive=False, verbosity=0) self.assertFalse(os.path.exists(orphan_file)) + def test_image_dimensions_corrupted_file(self): + original_filename = 'testimage.jpg' + file_obj = SimpleUploadedFile( + name=original_filename, + # corrupted! + content=create_image().tobytes(), + content_type='image/jpeg') + self.filer_image = Image.objects.create( + file=file_obj, + original_filename=original_filename) + + self.filer_image._width = 0 + self.filer_image.save() + + call_command('filer_check', image_dimensions=True) + self.filer_image.refresh_from_db() + self.assertNotEqual(self.filer_image._width, 1) + + def test_image_dimensions_file_not_found(self): + self.filer_image = Image.objects.create( + file="123.jpg", + original_filename="123.jpg") + call_command('filer_check', image_dimensions=True) + self.filer_image.refresh_from_db() + self.assertNotEqual(self.filer_image._width, 1) + def test_image_dimensions(self): original_filename = 'testimage.jpg'