SmileyChris / easy-thumbnails

Easy thumbnails for Django
http://easy-thumbnails.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
1.37k stars 312 forks source link

Does WebP workaround work with AWS S3? #577

Closed HenryMehta closed 2 years ago

HenryMehta commented 2 years ago

This is really just a question but I've implemented the WebP workaround proposed but it doesn't seem to work for me and I cannot understand what I've done wrong.

In my apps.py I have

from django.apps import AppConfig
from django .conf import settings 

import logging
logger = logging.getLogger('debug')

def store_as_webp(sender, **kwargs):
    '''
    Stores new images in next gen format for improved website performance (particularly mobile)
    '''
    logger.info(f'Signal store_as_webp called in core')
    logger.info(f'Name: {sender.name}')
    #webp_path = f'{settings.MEDIA_URL}{sender.name}.webp'
    webp_path = sender.storage.path('.'.join([sender.name, 'webp']))
    logger.info(f'webpath: {webp_path}')
    sender.image.save(webp_path, 'webp')
    logger.info('WebP Saved')

class CoreConfig(AppConfig):
    name = 'core'

    def ready(self):
        from easy_thumbnails.signals import thumbnail_created
        thumbnail_created.connect(store_as_webp)

The first 2 logger lines are printed, then nothing so something is going wrong but I do not understand what. Any help would be appreciated

AttilioGreco commented 2 years ago

You are not alone, i'm hitting the same issue have you find a solutions ?

Regars

HenryMehta commented 2 years ago

I did find the solution but cannot remember what it was. I'm running it on a Ubuntu PC and there was something I didn't have downloaded. Once done it worked fine. It was some webp program needed for Linux. I just don't remember what - sorry

AttilioGreco commented 2 years ago

I have analized with more details the problem. if you have the implementation reported in documentations is impossible it is worling with s3. sender.storage.path is not defined in s3 implementations, the it will return NotImplementedError rewrite store_as_webp as:

def store_as_webp(sender, **kwargs):
    s3_image_path = '.'.join([sender.name, 'webp'])
    _, fname = tempfile.mkstemp(dir="/tmp/")
    sender.image.save(fname, 'webp')
    f = open(fname, "rb")
    sender.storage.save(s3_image_path, f)
    f.close()
    os.unlink(fname)