diff options
author | Steven Day <steve@mysociety.org> | 2015-06-26 12:56:44 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2015-10-06 09:09:27 +0100 |
commit | a3b1269d17bcaeeabba5d81f92f8b7b2521a0fcb (patch) | |
tree | 94f2b51908955fd02110eecfe13e8c0fd7d40ffa /perllib/FixMyStreet/App | |
parent | 91dd9efadc255c240b13e7b26b69426ed5139ab9 (diff) |
Base 64 decode uploaded images if required
The new Zurich app has to base64 encode some images into a multipart
request so that it can (ab)use Phonegap's FileTransfer plugin to send
more than one image in a single request to /report/new/mobile.
Catalyst's Request::Upload module doesn't look at the
Content-Transfer-Encoding header inside multipart requests to do
this automatically, so we perform the decoding manually when we
process the images in the PhotoSet model.
Diffstat (limited to 'perllib/FixMyStreet/App')
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index 5d75b62dc..b18460821 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -8,6 +8,7 @@ use if !$ENV{TRAVIS}, 'Image::Magick'; use Scalar::Util 'openhandle', 'blessed'; use Digest::SHA qw(sha1_hex); use Image::Size; +use MIME::Base64; has c => ( is => 'ro', @@ -22,7 +23,7 @@ has data => ( # generic data from DB field 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 + # 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; @@ -117,6 +118,24 @@ has images => ( # AoA of [$fileid, $binary_data] tuples return (); } + # base64 decode the file if it's encoded that way + # Catalyst::Request::Upload doesn't do this automatically + # unfortunately. + my $transfer_encoding = $upload->headers->header('Content-Transfer-Encoding'); + if (defined $transfer_encoding && $transfer_encoding eq 'base64') { + my $decoded = decode_base64($upload->slurp); + if (open my $fh, '>', $upload->tempname) { + binmode $fh; + print $fh $decoded; + close $fh + } else { + my $c = $self->c; + $c->log->info('Couldn\'t open temp file to save base64 decoded image: ' . $!); + $c->stash->{photo_error} = _("Sorry, we couldn't save your image(s), please try again."); + return (); + } + } + # get the photo into a variable my $photo_blob = eval { my $filename = $upload->tempname; |