aboutsummaryrefslogtreecommitdiffstats
path: root/perllib
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2016-01-12 16:33:17 +0000
committerMatthew Somerville <matthew@mysociety.org>2016-01-12 17:31:38 +0000
commitdf98fe4dfff0a707c9800050f658100c57783654 (patch)
treefd39aa3cf82584fbf784334da41265b5f7dbc43c /perllib
parentde6df5e30d4801afc560e93c833a692a117b9547 (diff)
Factor multiple photo details into nicer function.
Remove get_photo_params, which only looked at the first photo, make explicit when we're doing that using `.first`.
Diffstat (limited to 'perllib')
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm70
-rw-r--r--perllib/FixMyStreet/App/Model/PhotoSet.pm5
-rw-r--r--perllib/FixMyStreet/Cobrand/Zurich.pm2
-rw-r--r--perllib/FixMyStreet/DB/Result/Comment.pm38
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm41
-rw-r--r--perllib/Utils/Photo.pm46
6 files changed, 65 insertions, 137 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index 5a5771488..df4cab2d8 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -4,11 +4,9 @@ use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'; }
-use DateTime::Format::HTTP;
-use Digest::SHA qw(sha1_hex);
+use JSON;
use File::Path;
use File::Slurp;
-use Path::Class;
use FixMyStreet::App::Model::PhotoSet;
use if !$ENV{TRAVIS}, 'Image::Magick';
@@ -35,16 +33,12 @@ sub during :LocalRegex('^([0-9a-f]{40})\.(temp|fulltemp)\.jpeg$') {
my ( $self, $c ) = @_;
my ( $hash, $size ) = @{ $c->req->captures };
- my $file = file( $c->config->{UPLOAD_DIR}, "$hash.jpeg" );
- my $photo = $file->slurp;
+ my $photoset = FixMyStreet::App::Model::PhotoSet->new({
+ data_items => [ $hash ]
+ });
- if ( $size eq 'temp' ) {
- if ( $c->cobrand->default_photo_resize ) {
- $photo = _shrink( $photo, $c->cobrand->default_photo_resize );
- } else {
- $photo = _shrink( $photo, '250x250' );
- }
- }
+ $size = $size eq 'temp' ? 'default' : 'full';
+ my $photo = $photoset->get_image_data(size => $size, default => $c->cobrand->default_photo_resize);
$c->forward( 'output', [ $photo ] );
}
@@ -73,29 +67,9 @@ sub index :LocalRegex('^(c/)?(\d+)(?:\.(\d+))?(?:\.(full|tn|fp))?\.jpeg$') {
$c->detach( 'no_photo' ) unless $c->cobrand->allow_photo_display($item); # Should only be for reports, not updates
my $photo;
- if ($item->can('get_photoset')) {
- $photo = $item->get_photoset
- ->get_image_data( num => $photo_number, size => $size, default => $c->cobrand->default_photo_resize )
+ $photo = $item->get_photoset
+ ->get_image_data( num => $photo_number, size => $size, default => $c->cobrand->default_photo_resize )
or $c->detach( 'no_photo' );
- } else {
- $photo = $item->photo;
- # If photo field contains a hash
- if (length($photo) == 40) {
- my $file = file( $c->config->{UPLOAD_DIR}, "$photo.jpeg" );
- $photo = $file->slurp;
- }
-
- if ( $size eq 'tn' ) {
- $photo = _shrink( $photo, 'x100' );
- } elsif ( $size eq 'fp' ) {
- $photo = _crop( $photo );
- } elsif ( $size eq 'full' ) {
- } elsif ( $c->cobrand->default_photo_resize ) {
- $photo = _shrink( $photo, $c->cobrand->default_photo_resize );
- } else {
- $photo = _shrink( $photo, '250x250' );
- }
- }
$c->forward( 'output', [ $photo ] );
}
@@ -116,34 +90,6 @@ sub no_photo : Private {
$c->detach( '/page_error_404_not_found', [ 'No photo' ] );
}
-# Shrinks a picture to the specified size, but keeping in proportion.
-sub _shrink {
- my ($photo, $size) = @_;
- my $image = Image::Magick->new;
- $image->BlobToImage($photo);
- my $err = $image->Scale(geometry => "$size>");
- throw Error::Simple("resize failed: $err") if "$err";
- $image->Strip();
- my @blobs = $image->ImageToBlob();
- undef $image;
- return $blobs[0];
-}
-
-# Shrinks a picture to 90x60, cropping so that it is exactly that.
-sub _crop {
- my ($photo) = @_;
- my $image = Image::Magick->new;
- $image->BlobToImage($photo);
- my $err = $image->Resize( geometry => "90x60^" );
- throw Error::Simple("resize failed: $err") if "$err";
- $err = $image->Extent( geometry => '90x60', gravity => 'Center' );
- throw Error::Simple("resize failed: $err") if "$err";
- $image->Strip();
- my @blobs = $image->ImageToBlob();
- undef $image;
- return $blobs[0];
-}
-
sub upload : Local {
my ( $self, $c ) = @_;
my @items = (
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm
index 9abe1dce7..35a7e8a53 100644
--- a/perllib/FixMyStreet/App/Model/PhotoSet.pm
+++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm
@@ -292,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) = @_;
diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm
index b7c9e9f45..ea4eda8c4 100644
--- a/perllib/FixMyStreet/Cobrand/Zurich.pm
+++ b/perllib/FixMyStreet/Cobrand/Zurich.pm
@@ -1172,7 +1172,7 @@ sub admin_stats {
$public_response =~ s{\r?\n}{ <br/> }g if $public_response;
# Assemble photo URL, if report has a photo
- my $media_url = $report->get_photo_params->{url} ? ($c->cobrand->base_url . $report->get_photo_params->{url}) : '';
+ my $media_url = @{$report->photos} ? ($c->cobrand->base_url . $report->photos->[0]->{url}) : '';
my @columns = (
$report->id,
diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm
index 41e8cf315..3ae56591f 100644
--- a/perllib/FixMyStreet/DB/Result/Comment.pm
+++ b/perllib/FixMyStreet/DB/Result/Comment.pm
@@ -97,7 +97,6 @@ __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
__PACKAGE__->rabx_column('extra');
use Moo;
-use Utils::Photo;
use namespace::clean -except => [ 'meta' ];
with 'FixMyStreet::Roles::Abuser';
@@ -148,15 +147,44 @@ sub confirm {
$self->confirmed( \'current_timestamp' );
}
-=head2 get_photo_params
+=head2 get_photoset
-Returns a hashref of details of any attached photo for use in templates.
+Return a PhotoSet object for all photos attached to this field
+
+ my $photoset = $obj->get_photoset;
+ print $photoset->num_images;
+ return $photoset->get_image_data(num => 0, size => 'full');
=cut
-sub get_photo_params {
+sub get_photoset {
+ my ($self) = @_;
+ my $class = 'FixMyStreet::App::Model::PhotoSet';
+ eval "use $class";
+ return $class->new({
+ db_data => $self->photo,
+ object => $self,
+ });
+}
+
+sub photos {
my $self = shift;
- return Utils::Photo::get_photo_params($self, 'c');
+ my $photoset = $self->get_photoset;
+ my $i = 0;
+ my $id = $self->id;
+ my @photos = map {
+ my $format = 'jpeg';
+ my $cachebust = substr($_, 0, 8);
+ {
+ id => $_,
+ url_temp => "/photo/$_.temp.$format",
+ url_temp_full => "/photo/$_.fulltemp.$format",
+ url => "/photo/c/$id.$i.$format?$cachebust",
+ url_full => "/photo/c/$id.$i.full.$format?$cachebust",
+ idx => $i++,
+ }
+ } map { $_->[0] } $photoset->all_images;
+ return \@photos;
}
=head2 meta_problem_state
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 745d88629..8f9e76ad6 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -160,7 +160,6 @@ __PACKAGE__->rabx_column('geocode');
use Moo;
use namespace::clean -except => [ 'meta' ];
use Utils;
-use Utils::Photo;
with 'FixMyStreet::Roles::Abuser',
'FixMyStreet::Roles::Extra';
@@ -483,22 +482,6 @@ sub admin_url {
return $cobrand->admin_base_url . '/report_edit/' . $self->id;
}
-=head2 get_photo_params
-
-Returns a hashref of details of the attached photo, if any, for use in templates.
-
-NB: this method only returns the first if there are multiple photos. Use
-get_photoset if you wish to access multiple photos.
-
-=cut
-
-sub get_photo_params {
- # use Carp 'cluck';
- # cluck "get_photo_params called"; # TEMPORARY die to make sure I've done right thing with Zurich templates
- my $self = shift;
- return Utils::Photo::get_photo_params($self, 'id');
-}
-
=head2 is_open
Returns 1 if the problem is in a open state otherwise 0.
@@ -835,7 +818,7 @@ sub as_hashref {
state_t => _( $self->state ),
used_map => $self->used_map,
is_fixed => $self->fixed_states->{ $self->state } ? 1 : 0,
- photo => $self->get_photo_params,
+ photos => [ map { $_->{url} } @{$self->photos} ],
meta => $self->confirmed ? $self->meta_line( $c ) : '',
confirmed_pp => $self->confirmed ? $c->cobrand->prettify_dt( $self->confirmed ): '',
created_pp => $c->cobrand->prettify_dt( $self->created ),
@@ -873,6 +856,28 @@ sub get_photoset {
});
}
+sub photos {
+ my $self = shift;
+ my $photoset = $self->get_photoset;
+ my $i = 0;
+ my $id = $self->id;
+ my @photos = map {
+ my $format = 'jpeg';
+ my $cachebust = substr($_, 0, 8);
+ {
+ id => $_,
+ url_temp => "/photo/$_.temp.$format",
+ url_temp_full => "/photo/$_.fulltemp.$format",
+ url => "/photo/$id.$i.$format?$cachebust",
+ url_full => "/photo/$id.$i.full.$format?$cachebust",
+ url_tn => "/photo/$id.$i.tn.$format?$cachebust",
+ url_fp => "/photo/$id.$i.fp.$format?$cachebust",
+ idx => $i++,
+ }
+ } map { $_->[0] } $photoset->all_images;
+ return \@photos;
+}
+
__PACKAGE__->has_many(
"admin_log_entries",
"FixMyStreet::DB::Result::AdminLog",
diff --git a/perllib/Utils/Photo.pm b/perllib/Utils/Photo.pm
deleted file mode 100644
index 17514f195..000000000
--- a/perllib/Utils/Photo.pm
+++ /dev/null
@@ -1,46 +0,0 @@
-package Utils::Photo;
-
-use Image::Size;
-
-=head2 get_photo_params
-
-Returns a hashref of details of any attached photo (the first, if multiple
-ones) for use in templates. Hashref contains height, width and url keys.
-
-=cut
-
-sub get_photo_params {
- my ($self, $key) = @_;
-
- return {} unless $self->photo;
-
- $key = ($key eq 'id') ? '' : "/$key";
-
- my $pre = "/photo$key/" . $self->id;
- my $post = '.jpeg';
- my $photo = {};
-
- if ($self->can('get_photoset')) {
- my $data = $self->get_photoset()->get_raw_image_data(0);
- my $fileid = $data->[0];
- $post .= '?' . $fileid;
- $photo->{url_full} = "$pre.full$post";
- } elsif (length($self->photo) == 40) {
- $post .= '?' . $self->photo;
- $photo->{url_full} = "$pre.full$post";
- # XXX Can't use size here because {url} (currently 250px height) may be
- # being used, but at this point it doesn't yet exist to find the width
- # $str = FixMyStreet->config('UPLOAD_DIR') . $self->photo . '.jpeg';
- } else {
- my $str = \$self->photo;
- ( $photo->{width}, $photo->{height} ) = Image::Size::imgsize( $str );
- }
-
- $photo->{url} = "$pre$post";
- $photo->{url_tn} = "$pre.tn$post";
- $photo->{url_fp} = "$pre.fp$post";
-
- return $photo;
-}
-
-1;