From 5d0b41cba593141244fd275f08a4f87a42d83066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20St=C3=A4hli?= Date: Sat, 4 Nov 2023 14:40:13 +0100 Subject: [PATCH] first draft: image dimensions update managemenet command --- filer/management/commands/filer_check.py | 26 ++++++++++++++++++++++++ tests/test_filer_check.py | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/filer/management/commands/filer_check.py b/filer/management/commands/filer_check.py index 3a2de61b3..e522b05cb 100644 --- a/filer/management/commands/filer_check.py +++ b/filer/management/commands/filer_check.py @@ -41,6 +41,13 @@ def add_arguments(self, parser): default=False, help="Delete references in database if files are missing in media folder.", ) + parser.add_argument( + '--image-dimensions', + action='store_true', + dest='image_dimensions', + default=False, + help="Look for images without dimensions set, set them accordingly.", + ) parser.add_argument( '--noinput', '--no-input', @@ -72,6 +79,8 @@ def handle(self, *args, **options): self.stdout.write("Aborted: Delete orphaned files from storage.") return self.verify_storages(options) + if options['image_dimensions']: + self.image_dimensions(options) def verify_references(self, options): from filer.models.filemodels import File @@ -112,3 +121,20 @@ def walk(prefix): filer_public = filer_settings.FILER_STORAGES['public']['main'] storage = import_string(filer_public['ENGINE'])() walk(filer_public['UPLOAD_TO_PREFIX']) + + def image_dimensions(self, options): + from filer.models.filemodels import Image + + no_dimensions = Image.objects.filter( + Q(_width=0) | Q(_width__isnull=True) + ) + self.stdout.write(f"trying to set dimensions on {no_dimensions.count()} files") + for image in no_dimensions: + try: + imgfile = image.file.file + except ValueError: + imgfile = image.file_ptr.file + imgfile.seek(0) + image._width, image._height = VILImage.load(imgfile).size + image.save() + return diff --git a/tests/test_filer_check.py b/tests/test_filer_check.py index 01b951ca0..2890cad42 100644 --- a/tests/test_filer_check.py +++ b/tests/test_filer_check.py @@ -67,3 +67,11 @@ 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(self): + self.filer_file._width = 0 + self.filer_file.save() + + call_command('filer_check', image_dimensions=True) + self.filer_file.refresh_from_db() + self.assertFalse(self.filer_file._width == 0)