blob: 9b0e5c9c3f6dac7e97c135fbadb492db38685a57 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package FixMyStreet::PhotoStorage;
use MIME::Base64;
use Moose;
use Digest::SHA qw(sha1_hex);
use Module::Load;
use FixMyStreet;
our $instance; # our, so tests can set to undef when testing different backends
sub backend {
return $instance if $instance;
my $class = 'FixMyStreet::PhotoStorage::';
$class .= FixMyStreet->config('PHOTO_STORAGE_BACKEND') || 'FileSystem';
load $class;
$instance = $class->new();
return $instance;
}
sub detect_type {
my ($self, $photo) = @_;
return 'jpeg' if $photo =~ /^\x{ff}\x{d8}/;
return 'png' if $photo =~ /^\x{89}\x{50}/;
return 'tiff' if $photo =~ /^II/;
return 'gif' if $photo =~ /^GIF/;
return '';
}
=head2 get_fileid
Calculates an identifier for a binary blob of photo data.
This is just the SHA1 hash of the blob currently.
=cut
sub get_fileid {
my ($self, $photo_blob) = @_;
return sha1_hex($photo_blob);
}
=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;
|