Commit | Line | Data |
146554c5 |
1 | package Catalyst::Request::Upload; |
2 | |
3 | use strict; |
4 | use base 'Class::Accessor::Fast'; |
5 | |
a2f2cde9 |
6 | use Catalyst::Exception; |
47ae6960 |
7 | use File::Copy (); |
8 | use IO::File (); |
3e22baa5 |
9 | use File::Spec::Unix; |
146554c5 |
10 | |
3e22baa5 |
11 | __PACKAGE__->mk_accessors(qw/filename headers size tempname type basename/); |
146554c5 |
12 | |
b549b049 |
13 | sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) } |
146554c5 |
14 | |
15 | =head1 NAME |
16 | |
bab3a22c |
17 | Catalyst::Request::Upload - handles file upload requests |
146554c5 |
18 | |
19 | =head1 SYNOPSIS |
20 | |
3e22baa5 |
21 | $upload->basename; |
bab3a22c |
22 | $upload->copy_to; |
23 | $upload->fh; |
146554c5 |
24 | $upload->filename; |
4be535b1 |
25 | $upload->headers; |
3ffaf022 |
26 | $upload->link_to; |
146554c5 |
27 | $upload->size; |
32d4bba8 |
28 | $upload->slurp; |
146554c5 |
29 | $upload->tempname; |
30 | $upload->type; |
31 | |
7257e9db |
32 | To specify where Catalyst should put the temporary files, set the 'uploadtmp' |
33 | option in the Catalyst config. If unset, Catalyst will use the system temp dir. |
34 | |
35 | __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' ); |
36 | |
146554c5 |
37 | See also L<Catalyst>. |
38 | |
39 | =head1 DESCRIPTION |
40 | |
bab3a22c |
41 | This class provides accessors and methods to handle client upload requests. |
146554c5 |
42 | |
43 | =head1 METHODS |
44 | |
b5ecfcf0 |
45 | =head2 $upload->new |
cd3bb248 |
46 | |
bab3a22c |
47 | Simple constructor. |
cd3bb248 |
48 | |
b5ecfcf0 |
49 | =head2 $upload->copy_to |
47ae6960 |
50 | |
bab3a22c |
51 | Copies the temporary file using L<File::Copy>. Returns true for success, |
52 | false for failure. |
47ae6960 |
53 | |
3ffaf022 |
54 | $upload->copy_to('/path/to/target'); |
55 | |
47ae6960 |
56 | =cut |
57 | |
3ffaf022 |
58 | sub copy_to { |
c462faf0 |
59 | my $self = shift; |
60 | return File::Copy::copy( $self->tempname, @_ ); |
47ae6960 |
61 | } |
62 | |
b5ecfcf0 |
63 | =head2 $upload->fh |
146554c5 |
64 | |
bab3a22c |
65 | Opens a temporary file (see tempname below) and returns an L<IO::File> handle. |
146554c5 |
66 | |
67 | =cut |
68 | |
69 | sub fh { |
70 | my $self = shift; |
71 | |
a2f2cde9 |
72 | my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY ); |
4be535b1 |
73 | |
a2f2cde9 |
74 | unless ( defined $fh ) { |
4be535b1 |
75 | |
a2f2cde9 |
76 | my $filename = $self->tempname; |
4be535b1 |
77 | |
a2f2cde9 |
78 | Catalyst::Exception->throw( |
4be535b1 |
79 | message => qq/Can't open '$filename': '$!'/ ); |
a2f2cde9 |
80 | } |
146554c5 |
81 | |
82 | return $fh; |
83 | } |
84 | |
b5ecfcf0 |
85 | =head2 $upload->filename |
146554c5 |
86 | |
bab3a22c |
87 | Returns the client-supplied filename. |
146554c5 |
88 | |
b5ecfcf0 |
89 | =head2 $upload->headers |
4be535b1 |
90 | |
bab3a22c |
91 | Returns an L<HTTP::Headers> object for the request. |
4be535b1 |
92 | |
b5ecfcf0 |
93 | =head2 $upload->link_to |
146554c5 |
94 | |
bab3a22c |
95 | Creates a hard link to the temporary file. Returns true for success, |
96 | false for failure. |
146554c5 |
97 | |
3ffaf022 |
98 | $upload->link_to('/path/to/target'); |
146554c5 |
99 | |
100 | =cut |
101 | |
3ffaf022 |
102 | sub link_to { |
47ae6960 |
103 | my ( $self, $target ) = @_; |
5c0ff128 |
104 | return CORE::link( $self->tempname, $target ); |
146554c5 |
105 | } |
106 | |
b5ecfcf0 |
107 | =head2 $upload->size |
146554c5 |
108 | |
bab3a22c |
109 | Returns the size of the uploaded file in bytes. |
146554c5 |
110 | |
b5ecfcf0 |
111 | =head2 $upload->slurp |
32d4bba8 |
112 | |
bab3a22c |
113 | Returns a scalar containing the contents of the temporary file. |
32d4bba8 |
114 | |
115 | =cut |
116 | |
117 | sub slurp { |
118 | my ( $self, $layer ) = @_; |
119 | |
4be535b1 |
120 | unless ($layer) { |
32d4bba8 |
121 | $layer = ':raw'; |
122 | } |
123 | |
124 | my $content = undef; |
125 | my $handle = $self->fh; |
126 | |
127 | binmode( $handle, $layer ); |
128 | |
129 | while ( $handle->sysread( my $buffer, 8192 ) ) { |
130 | $content .= $buffer; |
131 | } |
132 | |
133 | return $content; |
134 | } |
135 | |
3e22baa5 |
136 | sub basename { |
137 | my $self = shift; |
138 | unless ( $self->{basename} ) { |
139 | my $basename = $self->filename; |
140 | $basename =~ s|\\|/|g; |
141 | $basename = ( File::Spec::Unix->splitpath($basename) )[2]; |
142 | $basename =~ s|[^\w\.-]+|_|g; |
143 | $self->{basename} = $basename; |
144 | } |
145 | |
146 | return $self->{basename}; |
147 | } |
148 | |
149 | =head2 $upload->basename |
150 | |
151 | Returns basename for C<filename>. |
152 | |
b5ecfcf0 |
153 | =head2 $upload->tempname |
146554c5 |
154 | |
bab3a22c |
155 | Returns the path to the temporary file. |
146554c5 |
156 | |
b5ecfcf0 |
157 | =head2 $upload->type |
146554c5 |
158 | |
bab3a22c |
159 | Returns the client-supplied Content-Type. |
146554c5 |
160 | |
bab3a22c |
161 | =head1 AUTHORS |
146554c5 |
162 | |
7ea27c7c |
163 | Sebastian Riedel, C<sri@cpan.org> |
164 | |
165 | Christian Hansen, C<ch@ngmedia.com> |
146554c5 |
166 | |
167 | =head1 COPYRIGHT |
168 | |
169 | This program is free software, you can redistribute it and/or modify |
170 | it under the same terms as Perl itself. |
171 | |
172 | =cut |
173 | |
174 | 1; |