diff options
Diffstat (limited to 'perllib/FixMyStreet/PhotoStorage.pm')
-rw-r--r-- | perllib/FixMyStreet/PhotoStorage.pm | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/PhotoStorage.pm b/perllib/FixMyStreet/PhotoStorage.pm index a441fb718..558c93749 100644 --- a/perllib/FixMyStreet/PhotoStorage.pm +++ b/perllib/FixMyStreet/PhotoStorage.pm @@ -37,5 +37,33 @@ sub get_fileid { } +=head2 base64_decode_upload + +base64 decode the temporary on-disk uploaded file if +it's encoded that way. Modifies the file in-place. +Catalyst::Request::Upload doesn't do this automatically +unfortunately. + +=cut + +sub base64_decode_upload { + my ( $c, $upload ) = @_; + + 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 { + $c->log->info('Couldn\'t open temp file to save base64 decoded image: ' . $!); + $c->stash->{photo_error} = _("Sorry, we couldn't save your file(s), please try again."); + return (); + } + } + +} + 1; |