Skip to content

Created images not deleted when using image_key_post_processor #204

@nicksellen

Description

@nicksellen

I noticed some odd issues with updating images for karrot.world, which uses this library (code at https://github.com/karrot-dev/karrot-backend).

After uploading a new image it would sometimes not generate a new thumbnail for the image I just uploaded, but re-use an old one from an entirely different image.

I traced the issue down to the thumbnails not being deleted when we delete the main image, despite calling delete_all_created_images. That left the thumbnails still sitting around, and the cache thinking it has that thumbnail, so the next upload that comes along that uses the same filename it'll reuse the old thumbnail, whoops!

The reason it fails to delete the thumbnail is that when using image_key_post_processor set to versatileimagefield.processors.md5 it creates files such as:

media/__sized__/example_images/example_image-07f9487f0ca8e07fbb7aeb0fca600efb.jpg

But the deletion system uses regex and looks for a pattern like thumbnail-400x400... so it can't find the md5 hashed ones.

e.g. here's a bit where I added some logging inside the lib:

checking if -07f9487f0ca8e07fbb7aeb0fca600efb matches re.compile('-(crop-c[0-9-]+__[0-9-]+|thumbnail)-(\\d+)x(\\d+)(?:-\\d+)?$')

It doesn't match so never gets deleted.

I created a reproduction repo just to check it wasn't something we were doing oddly: https://github.com/nicksellen/django-versatileimagefield-repro

To re-create you can run this:

git clone https://github.com/nicksellen/django-versatileimagefield-repro.git
cd django-versatileimagefield-repro
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
./manage.py migrate
./manage.py reproduce

The output from that when I run it is:

/manage.py reproduce
reproduce
deleted all example models
created example model
created thumbnail media/__sized__/example_images/example_image-07f9487f0ca8e07fbb7aeb0fca600efb.jpg
thumbnail exists? True
ran delete_all_created_images()
OH NO, thumbanil still exists!

Sorry, I didn't write it as a proper test or something....

I think we have to disable that setting to get deletion to work again, and cleanup all the other files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions