pythondjangodjango-rest-frameworkeasy-thumbnails

Django easy-thumbnails getting weird naming with duplicate extensions


I'm using easy-thumbnails, the original file is saved correctly but the thumbnails are being saved with a duplicate file extension:

11.jpg
11.jpg.100x100_q85.jpg

I would like the thumbnails to be named like this:

11.100x100_q85.jpg

My model looks like this:

def image_filename(instance, filename):
    folder = 'posts/image'
    _, ext = os.path.splitext(filename)
    new_name = str(instance.id) + ext
    return os.path.join(folder, new_name)


class Post(models.Model):
    name = models.CharField(max_length=255, unique=True)
    image = ThumbnailerImageField(upload_to=image_filename, null=True, blank=True)

Since im using Django Rest Framework I created a serializer following this post: Django easy-thumbnails serialize with Django Rest Framework

class ThumbnailSerializer(serializers.ImageField):
    def __init__(self, alias, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.read_only = True
        self.alias = alias

    def to_representation(self, value):
        if not value:
            return None

        url = thumbnail_url(value, self.alias)
        request = self.context.get('request', None)
        if request is not None:
            return request.build_absolute_uri(url)

        return url

Does anyone know how I can get the correct naming on my thumbnails?

Thanks!


Solution

  • According to the docs of the library, Link. There are 4 namers available.

    Four namers are included in easy_thumbnails:
    
    ``easy_thumbnails.namers.default``
        Descriptive filename containing the source and options like
        ``source.jpg.100x100_q80_crop_upscale.jpg``.
    
    ``easy_thumbnails.namers.hashed``
        Short hashed filename like ``1xedFtqllFo9.jpg``.
    
    ``easy_thumbnails.namers.alias``
        Filename based on ``THUMBNAIL_ALIASES`` dictionary key like ``source.jpg.medium_large.jpg``.
    
    ``easy_thumbnails.namers.source_hashed``
        Filename with source hashed, size, then options hashed like
        ``1xedFtqllFo9_100x100_QHCa6G1l.jpg``.
    

    Since you didn't set it, it will take the default option which will give 11.jpg.100x100_q85.jpg.

    However you have the option to create a custom namer. Link

    To write a custom namer, always catch all other keyword arguments arguments
    (with \\*\\*kwargs). You have access to the following arguments:
    ``thumbnailer``, ``source_filename``, ``thumbnail_extension`` (does *not*
    include the ``'.'``), ``thumbnail_options``, ``prepared_options``.
    

    Also here Link

    Note: Someone also opened an issue at Github. Link