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