codingjoe / django-pictures

Responsive cross-browser image library using modern codes like AVIF & WebP
BSD 2-Clause "Simplified" License
248 stars 20 forks source link

delete orphaned images #23

Closed vchrisb closed 2 years ago

vchrisb commented 2 years ago

django-stdimage has an option to delete orphaned images (delete_orphans=True), when updating an image or deleting the associated image. It would be great if django-pictures would have a similar capability.

Additionality when removing an aspect_ratio from a model, the obsolete images are not deleted.

vchrisb commented 2 years ago

I looked at the code an there is not yet a post_delete signal attached.

vchrisb commented 2 years ago

@codingjoe do you want to have a delete orphaned functionality as part of this library or do you consider to document the usage of https://github.com/un1t/django-cleanup?

To be able to use django-cleanup, I only had to modify https://github.com/codingjoe/django-pictures/blob/main/pictures/models.py#L105 to check if also the instance has the width_field attribute. Django-cleanup is using a "FakeInstance". e.g.

    @property
    def width(self):
        self._require_file()
        if self._committed and self.field.width_field and hasattr(self.instance, self.field.width_field):
            # get width from width field, to avoid loading image
            return getattr(self.instance, self.field.width_field)
        return self._get_image_dimensions()[0]

Directories are not deleted currently.

codingjoe commented 2 years ago

@vchrisb this is the last feature on my list. However, cleanup seems mature and a better choice than a custom implementation. What do you think? Oh, and I'd be delighted if you find a second to implement this bit. :)

codingjoe commented 2 years ago

Resolved in ccc6ec5