diff --git a/fragdenstaat_de/fds_cms/apps.py b/fragdenstaat_de/fds_cms/apps.py index 069beade1..d7b2ce1a9 100644 --- a/fragdenstaat_de/fds_cms/apps.py +++ b/fragdenstaat_de/fds_cms/apps.py @@ -1,6 +1,9 @@ from django.apps import AppConfig from django.conf import settings +from easy_thumbnails.optimize import thumbnail_created_callback +from easy_thumbnails.signals import thumbnail_created + class FdsCmsConfig(AppConfig): name = "fragdenstaat_de.fds_cms" @@ -13,9 +16,10 @@ def ready(self): account_merged.connect(merge_user) - if settings.FDS_THUMBNAIL_ENABLE_AVIF: - from easy_thumbnails.signals import thumbnail_created + thumbnail_created.disconnect(thumbnail_created_callback) + thumbnail_created.connect(async_optimize_thumbnail) + if settings.FDS_THUMBNAIL_ENABLE_AVIF: thumbnail_created.connect(store_as_avif) @@ -32,3 +36,11 @@ def store_as_avif(sender, **kwargs): from .tasks import generate_avif_thumbnail generate_avif_thumbnail.delay(sender.name, sender.storage) + + +def async_optimize_thumbnail(sender, **kwargs): + from .tasks import optimize_thumbnail_task + + optimize_thumbnail_task.delay( + sender.name, sender.file, sender.storage, sender.thumbnail_options + ) diff --git a/fragdenstaat_de/fds_cms/tasks.py b/fragdenstaat_de/fds_cms/tasks.py index be803333b..db2c4ad84 100644 --- a/fragdenstaat_de/fds_cms/tasks.py +++ b/fragdenstaat_de/fds_cms/tasks.py @@ -4,7 +4,8 @@ from django.core.files.base import ContentFile from celery import shared_task -from easy_thumbnails.files import generate_all_aliases +from easy_thumbnails.files import ThumbnailFile, generate_all_aliases +from easy_thumbnails.optimize import optimize_thumbnail from PIL import Image try: @@ -35,3 +36,11 @@ def generate_avif_thumbnail(filepath: str, storage): out_file.seek(0) storage.save(avif_path, ContentFile(out_file.read())) logger.info("Done converting %s to avif", filepath) + + +@shared_task +def optimize_thumbnail_task(name, file, storage, thumbnail_options): + thumbnail = ThumbnailFile( + name=name, file=file, storage=storage, thumbnail_options=thumbnail_options + ) + optimize_thumbnail(thumbnail) diff --git a/fragdenstaat_de/settings/base.py b/fragdenstaat_de/settings/base.py index f8a232460..97e9ddcd7 100644 --- a/fragdenstaat_de/settings/base.py +++ b/fragdenstaat_de/settings/base.py @@ -42,7 +42,6 @@ def INSTALLED_APPS(self): "menus", "sekizai", # easy thumbnails comes from froide - "easy_thumbnails.optimize", "filer", "mptt", "logentry_admin",