-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
EnforcePhotoSizeOpen311PreSend.pm
59 lines (36 loc) · 1.51 KB
/
EnforcePhotoSizeOpen311PreSend.pm
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
package FixMyStreet::Roles::EnforcePhotoSizeOpen311PreSend;
use Moo::Role;
=head1 NAME
FixMyStreet::Roles::EnforcePhotoSizeOpen311PreSend - limit report photo sizes on open311 pre-send
=head1 SYNOPSIS
Applied to a cobrand class to shrink any images larger than a given size as an open311 pre-send action.
Oversized images are repeatedly shrunk until they conform.
A 'photo_size_limit_applied_<bytes>' metadata flag is set on the report to indicate it has been processed
and prevent reprocessing.
=cut
=head1 REQUIRED METHODS
=cut
=head2 per_photo_size_limit_for_report_in_bytes
Takes the report and the number of images.
Returns the max number of bytes for each photo on the report.
0 indicates no max to apply.
=cut
sub per_photo_size_limit_for_report_in_bytes { 0 }
sub open311_pre_send { }
after open311_pre_send => sub {
my ($self, $report, $open311) = @_;
my $photoset = $report->get_photoset;
return unless $photoset->num_images > 0;
my $limit = $self->per_photo_size_limit_for_report_in_bytes($report, $photoset->num_images);
return unless $limit > 0;
my $limit_applied_flag = "photo_size_limit_applied_" . $limit;
return if $report->get_extra_metadata($limit_applied_flag);
# Keep shrinking oversized images to 90% of their original size until they conform.
my ($new, $shrunk) = $photoset->shrink_all_to_size($limit, 90);
if ($shrunk) {
$report->update({ photo => $new });
}
$report->set_extra_metadata( $limit_applied_flag => 1 );
$report->update;
};
1;