11 |
use base qw/Arh::Action::CreatePicture/; |
use base qw/Arh::Action::CreatePicture/; |
12 |
|
|
13 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
14 |
|
use Imager; |
15 |
|
|
16 |
=head2 take_action |
=head2 take_action |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
|
my $buf_size = 8192; |
21 |
|
my $conf = Jifty->config->app('pictures') or die "no pictures"; |
22 |
|
my $path = $conf->{original_path} or die "no original_path"; |
23 |
|
my $scale = $conf->{scale} or die "no scale"; |
24 |
|
|
25 |
sub take_action { |
sub take_action { |
26 |
my $self = shift; |
my $self = shift; |
27 |
|
|
30 |
my $filename = scalar( $fh ); |
my $filename = scalar( $fh ); |
31 |
$filename =~ s/^.*([\/\\])([^\1]+)$/$2/; |
$filename =~ s/^.*([\/\\])([^\1]+)$/$2/; |
32 |
|
|
|
local $/; |
|
|
binmode $fh; |
|
|
my $content = scalar <$fh>; |
|
|
|
|
33 |
if ( $filename !~ m/\.(jpg|jpeg|png|gif|tif|tiff)$/i ) { |
if ( $filename !~ m/\.(jpg|jpeg|png|gif|tif|tiff)$/i ) { |
34 |
$self->result->error("unknown file type $filename"); |
$self->result->error("unknown file type $filename"); |
35 |
return; |
return; |
36 |
} |
} |
37 |
|
|
38 |
warn "## $self take_action filename: $filename (", length($content), " bytes)\n"; |
if ( ! -e $path ) { |
39 |
|
mkdir $path or die "can't create $path: $!"; |
40 |
|
} |
41 |
|
|
42 |
|
# FIXME add check of maximum upload size |
43 |
|
|
44 |
|
open(my $fh_out, '>', "$path/$filename" ) or die "can't open $path/$filename: $!"; |
45 |
|
my $buff = ' ' x $buf_size; |
46 |
|
while( read($fh, $buff, $buf_size) ) { |
47 |
|
print $fh_out $buff or die "can't write to $filename: $!"; |
48 |
|
} |
49 |
|
close($fh_out) or die "can't close $filename: $!"; |
50 |
|
|
51 |
|
warn "## $self take_action resize $filename [", -s "$path/$filename", " bytes]\n"; |
52 |
|
|
53 |
|
my $image = Imager->new; |
54 |
|
$image->read( file => "$path/$filename" ) or die $image->errstr; |
55 |
|
|
56 |
|
my $content; |
57 |
|
|
58 |
|
my $scaled_image = $image->scale( %$scale ) or die $image->errstr; |
59 |
|
undef $image; |
60 |
|
$scaled_image->write( |
61 |
|
data => \$content, |
62 |
|
type => 'jpeg', |
63 |
|
# jpegquality => 95, |
64 |
|
) or die $image->errstr; |
65 |
|
undef $scaled_image; |
66 |
|
|
67 |
|
warn "## resized to ",length($content), " bytes..."; |
68 |
|
|
69 |
$self->argument_value( 'filename' => $filename ); # needed for report_success |
$self->argument_value( 'filename' => $filename ); # needed for report_success |
70 |
$self->argument_value( 'content' => $content ); |
$self->argument_value( 'content' => $content ); |
88 |
|
|
89 |
sub report_success { |
sub report_success { |
90 |
my $self = shift; |
my $self = shift; |
91 |
$self->result->message( 'Created ' . $self->argument_value('filename') . ' with ' . length( $self->argument_value('content') ) . ' bytes' ); |
$self->result->message( 'Created ' . $self->argument_value('filename') . ' [' . length( $self->argument_value('content') ) . ' bytes]' ); |
92 |
warn "## report_success ", dump( $self->result ); |
warn "## report_success ", dump( $self->result ); |
93 |
} |
} |
94 |
|
|