aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Photo.pm
diff options
context:
space:
mode:
authorDave Whiteland <dave@mysociety.org>2012-05-29 15:57:41 +0100
committerDave Whiteland <dave@mysociety.org>2012-05-29 15:57:41 +0100
commit67da8efc720d2d0bd22bd9fe8655b7e983b35bb4 (patch)
tree38b8570647124df06c637d4b923f6010211ef328 /perllib/FixMyStreet/App/Controller/Photo.pm
parent40b3a51d33caefa8f5fb97ce9be18ef936c7e260 (diff)
parent131ff6e9bf3626d6a8fff6ae54669d250148a63a (diff)
Merge remote branch 'origin/master' into fmb-read-only
Conflicts: bin/send-reports perllib/FixMyStreet/Cobrand/Default.pm perllib/FixMyStreet/Cobrand/FixMyStreet.pm templates/web/fixmystreet/alert/index.html templates/web/fixmystreet/around/display_location.html web/cobrands/fixmystreet/_layout.scss web/js/map-OpenLayers.js
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Photo.pm')
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm91
1 files changed, 91 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index 5d5832b08..a6717fc33 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -5,6 +5,7 @@ use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'; }
use DateTime::Format::HTTP;
+use Digest::SHA1 qw(sha1_hex);
use Path::Class;
=head1 NAME
@@ -136,6 +137,96 @@ sub _crop {
return $blobs[0];
}
+=head2 process_photo
+
+Handle the photo - either checking and storing it after an upload or retrieving
+it from the cache.
+
+Store any error message onto 'photo_error' in stash.
+=cut
+
+sub process_photo : Private {
+ my ( $self, $c ) = @_;
+
+ return
+ $c->forward('process_photo_upload')
+ || $c->forward('process_photo_cache')
+ || 1; # always return true
+}
+
+sub process_photo_upload : Private {
+ my ( $self, $c ) = @_;
+
+ # check for upload or return
+ my $upload = $c->req->upload('photo')
+ || return;
+
+ # check that the photo is a jpeg
+ my $ct = $upload->type;
+ $ct =~ s/x-citrix-//; # Thanks, Citrix
+ # Had a report of a JPEG from an Android 2.1 coming through as a byte stream
+ unless ( $ct eq 'image/jpeg' || $ct eq 'image/pjpeg' || $ct eq 'application/octet-stream' ) {
+ $c->log->info('Bad photo tried to upload, type=' . $ct);
+ $c->stash->{photo_error} = _('Please upload a JPEG image only');
+ return;
+ }
+
+ # get the photo into a variable
+ my $photo_blob = eval {
+ my $filename = $upload->tempname;
+ my $out = `jhead -se -autorot $filename 2>&1`;
+ die _("Please upload a JPEG image only"."\n") if $out =~ /Not JPEG:/;
+ my $photo = $upload->slurp;
+ return $photo;
+ };
+ if ( my $error = $@ ) {
+ my $format = _(
+"That image doesn't appear to have uploaded correctly (%s), please try again."
+ );
+ $c->stash->{photo_error} = sprintf( $format, $error );
+ return;
+ }
+
+ # we have an image we can use - save it to the upload dir for storage
+ my $cache_dir = dir( $c->config->{UPLOAD_DIR} );
+ $cache_dir->mkpath;
+ unless ( -d $cache_dir && -w $cache_dir ) {
+ warn "Can't find/write to photo cache directory '$cache_dir'";
+ return;
+ }
+
+ my $fileid = sha1_hex($photo_blob);
+ $upload->copy_to( file($cache_dir, $fileid . '.jpeg') );
+
+ # stick the hash on the stash, so don't have to reupload in case of error
+ $c->stash->{upload_fileid} = $fileid;
+
+ return 1;
+}
+
+=head2 process_photo_cache
+
+Look for the upload_fileid parameter and check it matches a file on disk. If it
+does return true and put fileid on stash, otherwise false.
+
+=cut
+
+sub process_photo_cache : Private {
+ my ( $self, $c ) = @_;
+
+ # get the fileid and make sure it is just a hex number
+ my $fileid = $c->req->param('upload_fileid') || '';
+ $fileid =~ s{[^0-9a-f]}{}gi;
+ return unless $fileid;
+
+ my $file = file( $c->config->{UPLOAD_DIR}, "$fileid.jpeg" );
+ return unless -e $file;
+
+ $c->stash->{upload_fileid} = $fileid;
+ return 1;
+}
+
+
=head1 AUTHOR
Struan Donald