/[Arh]/lib/Arh/Action/UploadPicture.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/Arh/Action/UploadPicture.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74 - (show annotations)
Fri Apr 11 18:42:39 2008 UTC (15 years, 11 months ago) by dpavlin
File size: 2607 byte(s)
convert path to absolute so that fastcgi backend works
1 use strict;
2 use warnings;
3
4 =head1 NAME
5
6 Arh::Action::UploadPicture
7
8 =cut
9
10 package Arh::Action::UploadPicture;
11 use base qw/Arh::Action::CreatePicture/;
12
13 use Data::Dump qw/dump/;
14 use Image::Magick::Thumbnail::Simple;
15 use File::Slurp;
16 use Encode qw/decode/;
17
18 =head2 take_action
19
20 =cut
21
22 my $buf_size = 8192;
23 my $config = Jifty->config->app('pictures') or die "no pictures in config.yml";
24 my $path = $config->{path} or die "no path";
25
26 $path = Jifty::Util->absolute_path( $path );
27
28 sub take_action {
29 my $self = shift;
30
31 if ( my $fh = $self->argument_value('content') ) {
32
33 my $filename = scalar( $fh );
34 $filename =~ s/^.*([\/\\])([^\1]+)$/$2/;
35
36 if ( $filename !~ m/^(.+)\.(jpg|jpeg|png|gif|tif|tiff)$/i ) {
37 $self->result->error("unknown file type $filename");
38 return;
39 }
40 my ( $file, $type ) = ( $1, $2 );
41
42 foreach my $dir ( '', 'thumb', 'orig' ) {
43 my $p = "$path/$dir";
44 if ( ! -e $p ) {
45 mkdir $p or die "can't create $p:$!";
46 }
47 }
48
49 # FIXME add check of maximum upload size
50
51 my $path_orig = "$path/orig/$filename";
52
53 open(my $fh_out, '>', $path_orig ) or die "can't open $path_orig: $!";
54 my $buff = ' ' x $buf_size;
55 while( read($fh, $buff, $buf_size) ) {
56 print $fh_out $buff or die "can't write to $path_orig: $!";
57 }
58 close($fh_out) or die "can't close $path_orig: $!";
59
60 warn "## $self take_action resize $filename [", -s $path_orig, " bytes]\n";
61
62 my $thumb_name = $file . '.jpg';
63 my $thumb_path = "$path/thumb/$thumb_name";
64
65 my $t = Image::Magick::Thumbnail::Simple->new;
66 $t->thumbnail(
67 input => "$path/orig/$filename",
68 output => $thumb_path,
69 size => $config->{thumbnail} || 128,
70 );
71
72 my $content = read_file($thumb_path) or die "can't read $thumb_path: $!";
73
74 warn "## resized to $thumb_name ", -s $thumb_path, " == ", length($content), " bytes...";
75
76 $self->argument_value( 'filename' => $filename ); # needed for report_success
77 $self->argument_value( 'content' => $content );
78 $self->argument_value( 'thumbnail' => $thumb_name );
79 $self->argument_value( 'width' => $t->width );
80 $self->argument_value( 'height' => $t->height );
81
82 my $id = $self->SUPER::take_action( @_ );
83
84 # update database with correct filename (why do I need this?)
85 $self->record->set_filename( $filename );
86
87 return $id;
88
89 } else {
90 $self->result->error("No file uploaded!");
91 }
92
93 }
94
95 =head2 report_success
96
97 =cut
98
99 sub report_success {
100 my $self = shift;
101 $self->result->message( 'Uploaded ' . $self->argument_value('filename') . ' [' . length( $self->argument_value('content') ) . ' bytes]' );
102 warn "## report_success ", dump( $self->result );
103 }
104
105 1;
106

  ViewVC Help
Powered by ViewVC 1.1.26