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

Configurable Celery serialzier #510

Open michal-demecki-profil-software opened 3 years ago

michal-demecki-profil-software commented 3 years ago

Pickle is considered as an unsafe serializer, so support for e.g. application/json would be great. https://github.com/matthewwithanm/django-imagekit/blob/3317273401d65b1d2c70c635e22132998f957290/imagekit/cachefiles/backends.py#L147

vstoykov commented 3 years ago

I'll be very glad if someone is willing to provide a PR with this functionality.

hvdklauw commented 3 years ago

Ran into this, we only accept json as serializer, for obvious reasons.

The problem is that the file object that is passed around either is too complex in parameters to correctly serialize and deserialize.

If I could somehow get the ContentType of the model, the ID and the field_name then I could reconstruct it and simply call _generate() on the field.

hvdklauw commented 3 years ago

OK, did some searching and can get the model through: file.generator.source.instance

The problem is that I can't find the attribute name that needs to be generated:

I have a logo image field and multiple image specs that generate different thumbnails from that.

For now I can use celery-once to create a task only once per model, and then use the generator_registry.get_ids() to find out which generators there are for a given model get the model and generate the images one by one.

It works, but it's far from ideal.

q0w commented 3 years ago

Why can't we pass filepath to celery/any async shedule_generation method instead of file argument and then find the file by the filepath variable?

hvdklauw commented 3 years ago

Because you need more information on how to thumbnail the image, the file itself it not enough.

q0w commented 3 years ago

@hvdklauw i suggest just replacing file with filepath and when file itself is needed, find it by name. Nothing else would change

for example u can find file by name before that https://github.com/matthewwithanm/django-imagekit/blob/f15c7c105823712378d3ad2dafc6664969c627c1/imagekit/cachefiles/backends.py#L96

hvdklauw commented 3 years ago

The file there is an ImageCacheFile, which has all the parameters on how to generate a thumbnail, the original file alone is not enough because you might use the original to generate 3 or 4 different thumbnails, so which one are you generating?

q0w commented 3 years ago

So no solutions?

hvdklauw commented 3 years ago

I totally wrote my own solution that just starts a task with the app label and model name (from ContentTypes) and an id, then the task loads it and just generates all the thumbnails.