diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Model/PhotoSet.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 111 |
1 files changed, 65 insertions, 46 deletions
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index b18460821..54457bae9 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -14,27 +14,32 @@ has c => ( is => 'ro', ); +# The attached report, for using its ID has object => ( is => 'ro', ); -has data => ( # generic data from DB field +# If a PhotoSet is generated from a database row, db_data is set, which then +# 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 => ( # String of photo hashes is => 'ro', lazy => 1, default => sub { - # yes, this is a little circular: data -> data_items -> items -> data - # e.g. if not provided, then we're presumably uploading/etc., so calculate from - # the stored cached fileids - # (obviously if you provide none of these, then you'll get an infinite loop) 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 data or b) provided by photo upload +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 => { @@ -42,8 +47,7 @@ has data_items => ( # either a) split from data or b) provided by photo upload }, default => sub { my $self = shift; - my $data = $self->data - or return []; + my $data = $self->db_data or return []; return [$data] if (_jpeg_magic($data)); @@ -56,7 +60,7 @@ has upload_dir => ( lazy => 1, default => sub { my $self = shift; - my $cache_dir = path( $self->c->config->{UPLOAD_DIR} ); + my $cache_dir = path( FixMyStreet->config('UPLOAD_DIR') ); $cache_dir->mkpath; unless ( -d $cache_dir && -w $cache_dir ) { warn "Can't find/write to photo cache directory '$cache_dir'"; @@ -72,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; @@ -159,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)) { @@ -167,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); (); } @@ -201,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' ); @@ -218,7 +223,7 @@ sub get_image_data { } elsif ( $size eq 'full' ) { # do nothing } else { - $photo = _shrink( $photo, $self->c->cobrand->default_photo_resize || '250x250' ); + $photo = _shrink( $photo, $args{default} || '250x250' ); } return $photo; @@ -235,18 +240,37 @@ sub delete_cached { ); } +sub remove_images { + my ($self, $ids) = @_; + + my @images = $self->all_ids; + my $dec = 0; + for (sort { $a <=> $b } @$ids) { + splice(@images, $_ + $dec, 1); + --$dec; + } + + my $new_set = (ref $self)->new({ + data_items => \@images, + object => $self->object, + }); + + $self->delete_cached(); + + return $new_set->data; # e.g. new comma-separated fileid +} + 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, - c => $self->c, + data_items => \@images, object => $self->object, }); @@ -268,11 +292,6 @@ sub _rotate_image { } - - - -# NB: These 2 subs stolen from A::C::Photo, should be purged from there! -# # Shrinks a picture to the specified size, but keeping in proportion. sub _shrink { my ($photo, $size) = @_; |