--- lib/Arh/Action/UploadPicture.pm 2008/04/06 16:09:03 48 +++ lib/Arh/Action/UploadPicture.pm 2008/04/06 19:54:12 52 @@ -11,11 +11,17 @@ use base qw/Arh::Action::CreatePicture/; use Data::Dump qw/dump/; +use Imager; =head2 take_action =cut +my $buf_size = 8192; +my $conf = Jifty->config->app('pictures') or die "no pictures"; +my $path = $conf->{original_path} or die "no original_path"; +my $scale = $conf->{scale} or die "no scale"; + sub take_action { my $self = shift; @@ -24,16 +30,41 @@ my $filename = scalar( $fh ); $filename =~ s/^.*([\/\\])([^\1]+)$/$2/; - local $/; - binmode $fh; - my $content = scalar <$fh>; - if ( $filename !~ m/\.(jpg|jpeg|png|gif|tif|tiff)$/i ) { $self->result->error("unknown file type $filename"); return; } - warn "## $self take_action filename: $filename (", length($content), " bytes)\n"; + if ( ! -e $path ) { + mkdir $path or die "can't create $path: $!"; + } + + # FIXME add check of maximum upload size + + open(my $fh_out, '>', "$path/$filename" ) or die "can't open $path/$filename: $!"; + my $buff = ' ' x $buf_size; + while( read($fh, $buff, $buf_size) ) { + print $fh_out $buff or die "can't write to $filename: $!"; + } + close($fh_out) or die "can't close $filename: $!"; + + warn "## $self take_action resize $filename [", -s "$path/$filename", " bytes]\n"; + + my $image = Imager->new; + $image->read( file => "$path/$filename" ) or die $image->errstr; + + my $content; + + my $scaled_image = $image->scale( %$scale ) or die $image->errstr; + undef $image; + $scaled_image->write( + data => \$content, + type => 'jpeg', +# jpegquality => 95, + ) or die $image->errstr; + undef $scaled_image; + + warn "## resized to ",length($content), " bytes..."; $self->argument_value( 'filename' => $filename ); # needed for report_success $self->argument_value( 'content' => $content ); @@ -57,7 +88,7 @@ sub report_success { my $self = shift; - $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]' ); warn "## report_success ", dump( $self->result ); }