From f19221e0cd17d02159c01a6ddc2c55b28ce4d730 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 18 May 2016 11:17:53 +0100 Subject: Remove specific files rather than glob. This should improve performance when there are a lot of cached photos. --- perllib/FixMyStreet/App/Model/PhotoSet.pm | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index 030dbd5be..1c8a86e3a 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -254,14 +254,25 @@ sub get_image_data { sub delete_cached { my ($self) = @_; my $object = $self->object or return; + my $id = $object->id or return; my @dirs = ('web', 'photo'); push @dirs, 'c' if ref $object eq 'FixMyStreet::DB::Result::Comment'; - unlink glob FixMyStreet->path_to( - @dirs, - $object->id . '.*' - ); + # Old files without an index number; will always be .jpeg + foreach my $size ("", ".fp", ".tn", ".full") { + unlink FixMyStreet->path_to(@dirs, "$id$size.jpeg"); + } + + # New files with index number + my @images = $self->all_ids; + foreach (map [ $_, $images[$_] ], 0 .. $#images) { + my ($i, $file) = @$_; + my ($fileid, $type) = split /\./, $file; + foreach my $size ("", ".fp", ".tn", ".full") { + unlink FixMyStreet->path_to(@dirs, "$id.$i$size.$type"); + } + } } sub remove_images { -- cgit v1.2.3