Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Image dimensions update management command #1434

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions filer/management/commands/filer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -112,3 +121,30 @@ 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 tests.utils.custom_image.models import Image
from filer.models.imagemodels import Image
from django.db.models import Q
from easy_thumbnails.VIL import Image as VILImage
from filer.utils.compatibility import PILImage
import easy_thumbnails

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)
if image.file.name.endswith('.svg'):
image._width, image._height = VILImage.load(imgfile).size
else:
pil_image = PILImage.open(imgfile)
self._width, self._height = pil_image.size
benzkji marked this conversation as resolved.
Show resolved Hide resolved
self._transparent = easy_thumbnails.utils.is_transparent(pil_image)
benzkji marked this conversation as resolved.
Show resolved Hide resolved
image.save()
return
19 changes: 19 additions & 0 deletions tests/test_filer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from filer import settings as filer_settings
from filer.models.filemodels import File
from filer.models.imagemodels import Image
from tests.helpers import create_image


Expand Down Expand Up @@ -67,3 +68,21 @@ 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):

original_filename = 'testimage.jpg'
file_obj = SimpleUploadedFile(
name=original_filename,
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, 0)
Loading