diff options
author | Matthew Somerville <matthew@mysociety.org> | 2016-02-23 16:37:14 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2016-02-23 18:11:18 +0000 |
commit | 0d0b35dadf3ebf4781e00ae8d83b4061a99cfcc6 (patch) | |
tree | 722afef3118275cc9d952d45e89fbd8e75ba746a /perllib | |
parent | 28144f5153a0a7f7bad9466c883bd5c147568028 (diff) |
Only load images from disk when needed.
Fixes #1337.
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 72 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 2 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 3 |
5 files changed, 44 insertions, 47 deletions
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index 35a7e8a53..54457bae9 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -20,26 +20,26 @@ has object => ( ); # If a PhotoSet is generated from a database row, db_data is set, which then -# fills data_items -> images -> data. If it is generated during creation, -# data_items is set, which then similarly fills images -> data. +# fills data_items -> ids -> data. If it is generated during creation, +# data_items is set, which then similarly fills ids -> data. has db_data => ( # generic data from DB field is => 'ro', ); -has data => ( # List of photo hashes +has data => ( # String of photo hashes is => 'ro', lazy => 1, default => sub { my $self = shift; - my $data = join ',', map { $_->[0] } $self->all_images; + my $data = join ',', $self->all_ids; return $data; } ); has data_items => ( # either a) split from db_data or b) provided by photo upload isa => 'ArrayRef', - is => 'rw', + is => 'ro', traits => ['Array'], lazy => 1, handles => { @@ -76,33 +76,29 @@ sub _jpeg_magic { # and \x{49}\x{49} (Tiff, 3 results in live DB) ? } -=head2 C<images>, C<num_images>, C<get_raw_image_data>, C<all_images> +=head2 C<ids>, C<num_images>, C<get_id>, C<all_ids> -C<$photoset-E<GT>images> is an AoA containing the filed and the binary image data. +C<$photoset-E<GT>ids> is an arrayref containing the fileid data. - [ - [ $fileid1, $binary_data ], - [ $fileid2, $binary_data ], - ... - ] + [ $fileid1, $fileid2, ... ] Various accessors are provided onto it: num_images: count - get_raw_image_data ($index): return the [$fileid, $binary_data] tuple - all_images: return AoA as an array (e.g. rather than arrayref) + get_id ($index): return the correct id + all_ids: array of elements, rather than arrayref =cut -has images => ( # AoA of [$fileid, $binary_data] tuples +has ids => ( # Arrayref of $fileid tuples (always, so post upload/raw data processing) isa => 'ArrayRef', - is => 'rw', + is => 'ro', traits => ['Array'], lazy => 1, handles => { num_images => 'count', - get_raw_image_data => 'get', - all_images => 'elements', + get_id => 'get', + all_ids => 'elements', }, default => sub { my $self = shift; @@ -163,7 +159,7 @@ has images => ( # AoA of [$fileid, $binary_data] tuples my $fileid = $self->get_fileid($photo_blob); my $file = $self->get_file($fileid); $upload->copy_to( $file ); - return [$fileid, $photo_blob]; + return $fileid; } if (_jpeg_magic($part)) { @@ -171,21 +167,18 @@ has images => ( # AoA of [$fileid, $binary_data] tuples my $fileid = $self->get_fileid($photo_blob); my $file = $self->get_file($fileid); $file->spew_raw($photo_blob); - return [$fileid, $photo_blob]; + return $fileid; } if (length($part) == 40) { my $fileid = $part; my $file = $self->get_file($fileid); if ($file->exists) { - my $photo = $file->slurp_raw; - [$fileid, $photo]; - } - else { + $fileid; + } else { warn "File $fileid doesn't exist"; (); } - } - else { + } else { warn sprintf "Received bad photo hash of length %d", length($part); (); } @@ -205,15 +198,23 @@ sub get_file { return path( $cache_dir, "$fileid.jpeg" ); } +sub get_raw_image_data { + my ($self, $index) = @_; + my $fileid = $self->get_id($index); + my $file = $self->get_file($fileid); + if ($file->exists) { + my $photo = $file->slurp_raw; + return $photo; + } +} + sub get_image_data { my ($self, %args) = @_; my $num = $args{num} || 0; - my $data = $self->get_raw_image_data( $num ) + my $photo = $self->get_raw_image_data( $num ) or return; - my ($fileid, $photo) = @$data; - my $size = $args{size}; if ( $size eq 'tn' ) { $photo = _shrink( $photo, 'x100' ); @@ -242,16 +243,15 @@ sub delete_cached { sub remove_images { my ($self, $ids) = @_; - my @images = $self->all_images; + my @images = $self->all_ids; my $dec = 0; for (sort { $a <=> $b } @$ids) { splice(@images, $_ + $dec, 1); --$dec; } - my @items = map $_->[0], @images; my $new_set = (ref $self)->new({ - data_items => \@items, + data_items => \@images, object => $self->object, }); @@ -263,14 +263,14 @@ sub remove_images { sub rotate_image { my ($self, $index, $direction) = @_; - my @images = $self->all_images; + my @images = $self->all_ids; return if $index > $#images; - my @items = map $_->[0], @images; - $items[$index] = _rotate_image( $images[$index][1], $direction ); + my $image_data = $self->get_raw_image_data($index); + $images[$index] = _rotate_image( $image_data, $direction ); my $new_set = (ref $self)->new({ - data_items => \@items, + data_items => \@images, object => $self->object, }); diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index ea4eda8c4..c7e3c4d45 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -1014,23 +1014,21 @@ sub munge_sendreport_params { if ($row->state =~ /^(closed|investigating)$/ && $row->get_extra_metadata('publish_photo')) { # we attach images to reports sent to external bodies my $photoset = $row->get_photoset(); - my @images = $photoset->all_images + my $num = $photoset->num_images or return; - my $index = 0; my $id = $row->id; my @attachments = map { - my $i = $index++; { - body => $_->[1], + body => $photoset->get_raw_image_data($_), attributes => { - filename => "$id.$i.jpeg", + filename => "$id.$_.jpeg", content_type => 'image/jpeg', encoding => 'base64', # quoted-printable ends up with newlines corrupting binary data - name => "$id.$i.jpeg", + name => "$id.$_.jpeg", }, } - } @images; + } (0..$num-1); $params->{attachments} = \@attachments; } } diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 3ae56591f..5734ff8d5 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -183,7 +183,7 @@ sub photos { url_full => "/photo/c/$id.$i.full.$format?$cachebust", idx => $i++, } - } map { $_->[0] } $photoset->all_images; + } $photoset->all_ids; return \@photos; } diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 8f9e76ad6..2a90d0bec 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -874,7 +874,7 @@ sub photos { url_fp => "/photo/$id.$i.fp.$format?$cachebust", idx => $i++, } - } map { $_->[0] } $photoset->all_images; + } $photoset->all_ids; return \@photos; } diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 4b6d56146..11bc1e64f 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -125,8 +125,7 @@ sub update_comments { my $photoset = FixMyStreet::App::Model::PhotoSet->new({ data_items => [ $res->decoded_content ], }); - my $data = $photoset->get_raw_image_data(0); - $comment->photo($data->[0]); + $comment->photo($photoset->data); } } |