-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate avif thumbnails async in task
- Loading branch information
Showing
2 changed files
with
33 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
import logging | ||
from io import BytesIO | ||
|
||
from django.core.files.base import ContentFile | ||
|
||
from celery import shared_task | ||
from easy_thumbnails.files import generate_all_aliases | ||
from PIL import Image | ||
|
||
try: | ||
import pillow_avif # noqa | ||
except ImportError: | ||
pillow_avif = None | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task | ||
def generate_thumbnails(model, pk, field): | ||
instance = model._default_manager.get(pk=pk) | ||
fieldfile = getattr(instance, field) | ||
generate_all_aliases(fieldfile, include_global=True) | ||
|
||
|
||
@shared_task | ||
def generate_avif_thumbnail(filepath: str, storage): | ||
if pillow_avif is None: | ||
return | ||
logger.info("Converting %s to avif", filepath) | ||
avif_path = storage.path(".".join([filepath, "avif"])) | ||
img_file = storage.open(filepath, "rb") | ||
im = Image.open(img_file) | ||
out_file = BytesIO() | ||
im.save(out_file, format="avif", quality=80) | ||
out_file.seek(0) | ||
storage.save(avif_path, ContentFile(out_file.read())) | ||
logger.info("Done converting %s to avif", filepath) |