aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin.pm59
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm70
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm2
3 files changed, 55 insertions, 76 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm
index 0fc87c2f6..686d23aa1 100644
--- a/perllib/FixMyStreet/App/Controller/Admin.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin.pm
@@ -683,7 +683,7 @@ sub report_edit : Path('report_edit') : Args(1) {
}
if (my $rotate_photo_param = $self->_get_rotate_photo_param($c)) {
- $self->rotate_photo($c, @$rotate_photo_param);
+ $self->rotate_photo($c, $problem, @$rotate_photo_param);
if ( $c->cobrand->moniker eq 'zurich' ) {
# Clicking the photo rotation buttons should do nothing
# except for rotating the photo, so return the user
@@ -790,11 +790,12 @@ sub report_edit : Path('report_edit') : Args(1) {
}
# Deal with photos
- if ( $c->get_param('remove_photo') ) {
- $problem->photo(undef);
+ my $remove_photo_param = $self->_get_remove_photo_param($c);
+ if ($remove_photo_param) {
+ $self->remove_photo($c, $problem, $remove_photo_param);
}
- if ( $c->get_param('remove_photo') || $new_state eq 'hidden' ) {
+ if ( $remove_photo_param || $new_state eq 'hidden' ) {
unlink glob FixMyStreet->path_to( 'web', 'photo', $problem->id . '.*' );
}
@@ -974,6 +975,11 @@ sub update_edit : Path('update_edit') : Args(1) {
$c->stash->{update} = $update;
+ if (my $rotate_photo_param = $self->_get_rotate_photo_param($c)) {
+ $self->rotate_photo($c, $update, @$rotate_photo_param);
+ return 1;
+ }
+
$c->forward('check_email_for_abuse', [ $update->user->email ] );
if ( $c->get_param('banuser') ) {
@@ -1003,13 +1009,14 @@ sub update_edit : Path('update_edit') : Args(1) {
|| $c->get_param('anonymous') ne $update->anonymous
|| $c->get_param('text') ne $update->text ) {
$edited = 1;
- }
+ }
- if ( $c->get_param('remove_photo') ) {
- $update->photo(undef);
+ my $remove_photo_param = $self->_get_remove_photo_param($c);
+ if ($remove_photo_param) {
+ $self->remove_photo($c, $update, $remove_photo_param);
}
- if ( $c->get_param('remove_photo') || $new_state eq 'hidden' ) {
+ if ( $remove_photo_param || $new_state eq 'hidden' ) {
unlink glob FixMyStreet->path_to( 'web', 'photo', 'c', $update->id . '.*' );
}
@@ -1488,22 +1495,48 @@ sub _get_rotate_photo_param {
my $key = first { /^rotate_photo/ } keys %{ $c->req->params } or return;
my ($index) = $key =~ /(\d+)$/;
my $direction = $c->get_param($key);
- return [ $index || 0, $key, $direction ];
+ return [ $index || 0, $direction ];
}
sub rotate_photo : Private {
- my ( $self, $c, $index, $key, $direction ) = @_;
+ my ( $self, $c, $object, $index, $direction ) = @_;
return unless $direction eq _('Rotate Left') or $direction eq _('Rotate Right');
- my $problem = $c->stash->{problem};
- my $fileid = $problem->get_photoset($c)->rotate_image(
+ my $fileid = $object->get_photoset->rotate_image(
$index,
$direction eq _('Rotate Left') ? -90 : 90
) or return;
- $problem->update({ photo => $fileid });
+ $object->update({ photo => $fileid });
+
+ return 1;
+}
+
+=head2 remove_photo
+
+Remove a photo from a report
+=cut
+
+# Returns index of photo(s) to remove, if any
+sub _get_remove_photo_param {
+ my ($self, $c) = @_;
+
+ return 'ALL' if $c->get_param('remove_photo');
+
+ my @keys = map { /(\d+)$/ } grep { /^remove_photo_/ } keys %{ $c->req->params } or return;
+ return \@keys;
+}
+
+sub remove_photo : Private {
+ my ($self, $c, $object, $keys) = @_;
+ if ($keys eq 'ALL') {
+ $object->photo(undef);
+ } else {
+ my $fileids = $object->get_photoset->remove_images($keys);
+ $object->photo($fileids);
+ }
return 1;
}
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index f4866cc26..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( $c )
- ->get_image_data( num => $photo_number, size => $size )
+ $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/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index 4632f450d..db524ada4 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -449,7 +449,7 @@ sub initialize_report : Private {
$c->stash->{partial_token} = $token if $report;
# Stash the photo IDs for "already got" display
- $c->stash->{upload_fileid} = $report->get_photoset($c)->data;
+ $c->stash->{upload_fileid} = $report->get_photoset->data;
} else {
# no point keeping it if it is done.