Skip to content

Commit

Permalink
[#1931] delete associated thumbnails when a media image is deleted
Browse files Browse the repository at this point in the history
This calls the parent method to do the deletion as before,
then looks for other versions of the same image to also
delete from storage once the original is gone.

Fixes #1931.
  • Loading branch information
kareila committed Feb 9, 2017
1 parent 5d780ef commit 774f909
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 0 additions & 1 deletion cgi-bin/DW/Media/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ sub delete {
my $self = $_[0];
return 0 if $self->is_deleted;

# we need a mogilefs client or we can't edit media
my $u = $self->u
or croak 'Sorry, unable to load the user.';

Expand Down
41 changes: 41 additions & 0 deletions cgi-bin/DW/Media/Photo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,45 @@ sub _select_version {
# the resized image.
}

# this adds on to the base method by also deleting any associated thumbnails
sub delete {
my $self = $_[0];
my $deleted = $self->SUPER::delete;
return 0 unless $deleted; # was already deleted

# at this point the image has just been deleted - look for thumbnails
my $u = $self->u
or croak 'Sorry, unable to load the user.';
my @mv = $u->selectrow_array( "SELECT mediaid FROM media_versions" .
" WHERE userid=? AND versionid=?", undef,
$u->id, $self->versionid );
# this shouldn't happen unless something was saved in an incomplete state
# or the database has taken an unexpected vacation
return $deleted unless @mv;
my ( $mediaid ) = @mv;

if ( $mediaid == $self->versionid ) {
# find any resized versions and queue them for deletion as well
@mv = $u->selectrow_array(
"SELECT versionid FROM media_versions WHERE userid=? AND mediaid=?" .
" AND mediaid != versionid", undef, $u->id, $mediaid );
return $deleted unless @mv; # no other versions found

foreach my $id ( @mv ) {
# create a fake object to get the mogkey
my $fakeobj = bless { userid => $u->id, versionid => $id },
'DW::Media::Photo';
# we aren't concerned whether the file existed or not
DW::BlobStore->delete( media => $fakeobj->mogkey );
}

return 1; # done

} else {
# this wasn't the original, don't do anything else
return $deleted;
}
}


1;

0 comments on commit 774f909

Please sign in to comment.