Skip to content

Commit

Permalink
catch file not fonund and corrupt images
Browse files Browse the repository at this point in the history
  • Loading branch information
benzkji committed Nov 6, 2023
1 parent f065b98 commit 8b73704
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
21 changes: 15 additions & 6 deletions filer/management/commands/filer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'file_holder' is unnecessary as it is
redefined
before this value is used.
else:
file_holder = image

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'file_holder' is unnecessary as it is
redefined
before this value is used.

Check warning on line 143 in filer/management/commands/filer_check.py

View check run for this annotation

Codecov / codecov/patch

filer/management/commands/filer_check.py#L143

Added line #L143 was not covered by tests
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

Check warning on line 150 in filer/management/commands/filer_check.py

View check run for this annotation

Codecov / codecov/patch

filer/management/commands/filer_check.py#L150

Added line #L150 was not covered by tests
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
26 changes: 26 additions & 0 deletions tests/test_filer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8b73704

Please sign in to comment.