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