django-imagekit icon indicating copy to clipboard operation
django-imagekit copied to clipboard

Configurable Celery serialzier

Open michal-demecki-profil-software opened this issue 3 years ago • 9 comments

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

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

vstoykov avatar Feb 13 '21 23:02 vstoykov

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 avatar Jun 28 '21 12:06 hvdklauw

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.

hvdklauw avatar Jun 29 '21 11:06 hvdklauw

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?

q0w avatar Aug 16 '21 12:08 q0w

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

hvdklauw avatar Aug 16 '21 13:08 hvdklauw

@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

q0w avatar Aug 16 '21 13:08 q0w

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?

hvdklauw avatar Aug 16 '21 14:08 hvdklauw

So no solutions?

q0w avatar Aug 16 '21 14:08 q0w

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.

hvdklauw avatar Aug 16 '21 14:08 hvdklauw