Search code examples
pythondjangodjango-appsdjango-models

Django easy-thumbnails MAX SIZE


is there a way to set a max size for the images uploaded in my django app using easy-thumbnails app?

In the settings list I don't see anything about it.


Solution

  • To cap file size, you might want to do it in the webserver, rather than in Django.

    Alternatively, you can specify a custom file handler, with which you can raise an error if the file is too big:

    from django.core.files.uploadhandler import TemporaryFileUploadHandler, StopUpload
    
    class SizeLimitUploadHandler(TemporaryFileUploadHandler):
        def new_file(self, field_name, file_name, content_type, content_length, charset):
            if content_length > MAX_FILE_SIZE:
                raise StopUpload(True)
    

    Though this will cause a connection reset error in order to stop processing the large file.

    If you want to cap image size, you can resize the image before it is saved as stated in the readme:

    By passing a resize_source argument to the ThumbnailerImageField, you can resize the source image before it is saved:

    class Profile(models.Model):
        user = models.ForeignKey('auth.User')
        avatar = ThumbnailerImageField(
            upload_to='avatars',
            resize_source=dict(size=(50, 50), crop='smart'),
        )