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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
package FixMyStreet::App::Model::PhotoSet;
# TODO this isn't a Cat model, rename to something else
use Moose;
use Scalar::Util 'openhandle', 'blessed';
use Image::Size;
use IPC::Cmd qw(can_run);
use IPC::Open3;
use Try::Tiny;
use FixMyStreet;
use FixMyStreet::ImageMagick;
use FixMyStreet::PhotoStorage;
# Attached Catalyst app, if present, for feeding back errors during photo upload
has c => (
is => 'ro',
);
# The attached report, for using its ID
has object => (
is => 'ro',
);
# If a PhotoSet is generated from a database row, db_data is set, which then
# fills data_items -> ids -> data. If it is generated during creation,
# data_items is set, which then similarly fills ids -> data.
has db_data => ( # generic data from DB field
is => 'ro',
);
has data => ( # String of photo hashes
is => 'ro',
lazy => 1,
default => sub {
my $self = shift;
my $data = join ',', $self->all_ids;
return $data;
}
);
has data_items => ( # either a) split from db_data or b) provided by photo upload
isa => 'ArrayRef',
is => 'ro',
traits => ['Array'],
lazy => 1,
handles => {
map_data_items => 'map',
},
default => sub {
my $self = shift;
my $data = $self->db_data or return [];
return [$data] if ($self->storage->detect_type($data));
return [ split ',' => $data ];
},
);
has storage => (
is => 'ro',
lazy => 1,
default => sub {
return FixMyStreet::PhotoStorage::backend;
}
);
has symlinkable => (
is => 'ro',
lazy => 1,
default => sub {
my $cfg = FixMyStreet->config('PHOTO_STORAGE_OPTIONS');
return $cfg ? $cfg->{SYMLINK_FULL_SIZE} : 0;
}
);
=head2 C<ids>, C<num_images>, C<get_id>, C<all_ids>
C<$photoset-E<GT>ids> is an arrayref containing the fileid data.
[ $fileid1, $fileid2, ... ]
Various accessors are provided onto it:
num_images: count
get_id ($index): return the correct id
all_ids: array of elements, rather than arrayref
=cut
has ids => ( # Arrayref of $fileid tuples (always, so post upload/raw data processing)
isa => 'ArrayRef',
is => 'ro',
traits => ['Array'],
lazy => 1,
handles => {
num_images => 'count',
get_id => 'get',
all_ids => 'elements',
},
default => sub {
my $self = shift;
my @photos = $self->map_data_items( sub {
my $part = $_;
if (blessed $part and $part->isa('Catalyst::Request::Upload')) {
my $upload = $part;
my $ct = $upload->type;
$ct =~ s/x-citrix-//; # Thanks, Citrix
my ($type) = $ct =~ m{image/(jpeg|pjpeg|gif|tiff|png)};
$type = 'jpeg' if $type && $type eq 'pjpeg';
# Had a report of a JPEG from an Android 2.1 coming through as a byte stream
$type = 'jpeg' if !$type && $ct eq 'application/octet-stream';
unless ( $type ) {
my $c = $self->c;
$c->log->info('Bad photo tried to upload, type=' . $ct);
$c->stash->{photo_error} = _('Please upload an image only');
return ();
}
# Make sure any base64 encoding is handled.
FixMyStreet::PhotoStorage::base64_decode_upload($self->c, $upload);
# get the photo into a variable
my $photo_blob = eval {
my $filename = $upload->tempname;
my $out;
if ($type eq 'jpeg' && can_run('jhead')) {
my $pid = open3(undef, my $stdout, undef, 'jhead', '-se', '-autorot', $filename);
$out = join('', <$stdout>);
waitpid($pid, 0);
close $stdout;
}
unless (defined $out) {
my ($w, $h, $err) = Image::Size::imgsize($filename);
die _("Please upload an image only") . "\n" if !defined $w || $err !~ /JPG|GIF|PNG|TIF/;
}
die _("Please upload an image only") . "\n" if $out && $out =~ /Not JPEG:/;
my $photo = $upload->slurp;
};
if ( my $error = $@ ) {
my $format = _(
"That image doesn't appear to have uploaded correctly (%s), please try again."
);
$self->c->stash->{photo_error} = sprintf( $format, $error );
return ();
}
# we have an image we can use - save it to storage
$photo_blob = try {
FixMyStreet::ImageMagick->new(blob => $photo_blob)->shrink('2048x2048')->as_blob;
} catch { $photo_blob };
return $self->storage->store_photo($photo_blob);
}
# It might be a raw file stored in the DB column...
if (my $type = $self->storage->detect_type($part)) {
my $photo_blob = $part;
return $self->storage->store_photo($photo_blob);
# TODO: Should this update the DB record with a pointer to the
# newly-stored file, instead of leaving it in the DB?
}
if (my $key = $self->storage->validate_key($part)) {
$key;
} else {
# A bad hash, probably a bot spamming with bad data.
();
}
});
return \@photos;
},
);
sub get_raw_image {
my ($self, $index) = @_;
my $filename = $self->get_id($index);
my ($photo, $type, $object) = $self->storage->retrieve_photo($filename);
if ($photo) {
return {
$object ? (object => $object) : (),
data => $photo,
content_type => "image/$type",
extension => $type,
};
}
}
sub get_image_data {
my ($self, %args) = @_;
my $num = $args{num} || 0;
my $image = $self->get_raw_image( $num )
or return;
my $size = $args{size};
if ($self->symlinkable && $image->{object} && $size eq 'full') {
$image->{symlink} = delete $image->{object};
return $image;
}
my $im = FixMyStreet::ImageMagick->new(blob => $image->{data});
my $photo = try {
if ( $size eq 'tn' ) {
$im->shrink('x100');
} elsif ( $size eq 'fp' ) {
$im->crop;
} elsif ( $size eq 'og' ) {
$im->crop('1200x630');
} elsif ( $size eq 'full' ) {
$im
} else {
$im->shrink($args{default} || '250x250');
}
};
return unless $photo;
return {
data => $photo->as_blob,
width => $photo->width,
height => $photo->height,
content_type => $image->{content_type},
};
}
sub delete_cached {
my ($self, %params) = @_;
my $object = $self->object or return;
my $id = $object->id or return;
my @dirs = ('web', 'photo');
push @dirs, 'c' if ref $object eq 'FixMyStreet::DB::Result::Comment';
# Old files without an index number; will always be .jpeg
foreach my $size ("", ".fp", ".tn", ".full") {
unlink FixMyStreet->path_to(@dirs, "$id$size.jpeg");
}
# New files with index number
my @images = $self->all_ids;
foreach (map [ $_, $images[$_] ], 0 .. $#images) {
my ($i, $file) = @$_;
my ($fileid, $type) = split /\./, $file;
foreach my $size ("", ".fp", ".tn", ".full") {
unlink FixMyStreet->path_to(@dirs, "$id.$i$size.$type");
}
}
# Loop through all the updates as well if requested
if ($params{plus_updates}) {
$_->get_photoset->delete_cached() foreach $object->comments->all;
}
}
sub remove_images {
my ($self, $ids) = @_;
my @images = $self->all_ids;
my $dec = 0;
for (sort { $a <=> $b } @$ids) {
splice(@images, $_ + $dec, 1);
--$dec;
}
$self->delete_cached();
return undef if !@images;
my $new_set = (ref $self)->new({
data_items => \@images,
object => $self->object,
});
return $new_set->data; # e.g. new comma-separated fileid
}
sub rotate_image {
my ($self, $index, $direction) = @_;
my @images = $self->all_ids;
return if $index > $#images;
my $image = $self->get_raw_image($index);
$images[$index] = FixMyStreet::ImageMagick->new(blob => $image->{data})->rotate($direction)->as_blob;
my $new_set = (ref $self)->new({
data_items => \@images,
object => $self->object,
});
$self->delete_cached();
return $new_set->data; # e.g. new comma-separated fileid
}
1;
|