Skip to content

Commit

Permalink
⚡️ Run thumbnail optimization in task
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Mar 14, 2024
1 parent 45ff20b commit eacee7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 14 additions & 2 deletions fragdenstaat_de/fds_cms/apps.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)


Expand All @@ -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
)
11 changes: 10 additions & 1 deletion fragdenstaat_de/fds_cms/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
1 change: 0 additions & 1 deletion fragdenstaat_de/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def INSTALLED_APPS(self):
"menus",
"sekizai",
# easy thumbnails comes from froide
"easy_thumbnails.optimize",
"filer",
"mptt",
"logentry_admin",
Expand Down

0 comments on commit eacee7d

Please sign in to comment.