matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

ProcessedImageField file names not changing #436

Closed BojanKogoj closed 6 years ago

BojanKogoj commented 6 years ago

I am using ProcessedImageField and when image is uploaded (through API) it doesn't change file name, it uses the one it had before. Which is a problem, mobile devices resize and rename image, so I always receive avatar.jpeg. On local setup it will rename to avatar_jqWGWa5.png which is fine, however on server it saves as avatar.png. It might have something to do with S3, is there any way I can rename image before saving it?

vstoykov commented 6 years ago

The only purpose of ProcessedImageField compared to normal ImageField from Django is to apply specified processors on the uploaded image before saving it in the storage. By default it will preserve the file name with which the image was upladed. Django is adding some stuff at the end only if there is already a file with the same name (but this need to be implemented in the storage https://github.com/django/django/blob/stable/1.11.x/django/core/files/storage.py#L342-L347).

You said that you are using S3 on production. If the storage that you are using does not prevent you from overwriting a file then the rename will not happen. You need to look at the source of the storage that you are using for saving files to S3 in the _save() method to see if it will prevent it or not.

If you do not care about original file name then you can set upload_to keyword argument of ProcessedImageField/ImageField to be some function which will generate random names (with uuid module for example). By this you can be sure that there will be no file name conflict when new image is uploaded, no mater if the storage will allow overwriting of files or not.

BojanKogoj commented 6 years ago

Thank you for your help, I ended up creating custom method for upload_to like you suggested.