SmileyChris / easy-thumbnails

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

ThumbnailerImageField - File naming with duplicate file extensions (1.jpg.100x100_q85.jpg) #566

Closed adrenaline681 closed 1 year ago

adrenaline681 commented 3 years ago

When using the ThumbnailerImageField, the original file is saved correctly but the thumbnails are being saved with a duplicate file extension:

Example:

1.jpg
1.jpg.100x100_q85.jpg

I would like the thumbnails to be named like this:

1.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 I'm 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

Why is the extension not being removed from the filename before adding the ".100x100_q85.jpg"

MohammadKurjieh commented 3 years ago

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 1.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