Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cached formatted Images not deleted when image file renamed #3045

Closed
sheadawson opened this issue Apr 17, 2014 · 7 comments
Closed

Cached formatted Images not deleted when image file renamed #3045

sheadawson opened this issue Apr 17, 2014 · 7 comments

Comments

@sheadawson
Copy link

A bit of an edge case, but there is an issue where, for example - If I upload 2 images, image-1.jpg and image-2.jpg... and then renamed image-1.jpg to image-1a.jpg, and renamed image-2.jpg to "image-1.jpg", any resampled image-1.jpg images with the same resampling method and sizes will refer to a cached version of the original image-1.jpg, which is actually a different image.

A possible solution for this would be to deleteFormattedImages when an image file is renamed. Here's an extension I wrote to get around it

class CustomImage extends DataExtension{

    public function onBeforeWrite(){
        // Delete formatted images if the filename has changed
        $changed = $this->owner->getChangedFields();
        if(isset($changed['Name'])){
            $this->owner->Name = $changed['Name']['before'];
            $this->owner->Filename = $changed['Filename']['before'];
            $this->owner->deleteFormattedImages();
            $this->owner->Name = $changed['Name']['after'];
            $this->owner->Filename = $changed['Filename']['after'];
        }
    }
}  
@micmania1
Copy link
Contributor

@sheadawson This would probably solve silverstripe/silverstripe-cms#997 too. Sounds like the same issue.

@tractorcow
Copy link
Contributor

I would be careful of changing the data fields during an onbeforewrite, but the methodology seems ok. Maybe clone the object just to be safe.

You should also take into consideration that the file may be moved to another parent directory.

@sheadawson
Copy link
Author

Both excellent points @tractorcow

@kinglozzer kinglozzer added the 3.1 label Oct 25, 2014
@tractorcow
Copy link
Contributor

This is taken care of in 4.x. On move / rename, all variants are also moved.

@dhensby
Copy link
Contributor

dhensby commented Jul 27, 2017

@tractorcow did your recent patch cause any regressions in this regard?

@dhensby
Copy link
Contributor

dhensby commented Jul 27, 2017

@tractorcow
Copy link
Contributor

In 4.x if you rename a file, all variants are renamed at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants