Commit | Line | Data |
146554c5 |
1 | package Catalyst::Request::Upload; |
2 | |
e5ecd5bc |
3 | use Moose; |
531f1ab6 |
4 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
146554c5 |
5 | |
a2f2cde9 |
6 | use Catalyst::Exception; |
47ae6960 |
7 | use File::Copy (); |
89cb63ec |
8 | use IO::File (); |
3e22baa5 |
9 | use File::Spec::Unix; |
554c7587 |
10 | use namespace::clean -except => 'meta'; |
146554c5 |
11 | |
5fb12dbb |
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'); |
02570318 |
17 | has basename => (is => 'ro', lazy_build => 1); |
059c085b |
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 | |
02570318 |
37 | sub _build_basename { |
38 | my $self = shift; |
39 | my $basename = $self->filename; |
40 | $basename =~ s|\\|/|g; |
41 | $basename = ( File::Spec::Unix->splitpath($basename) )[2]; |
42 | $basename =~ s|[^\w\.-]+|_|g; |
43 | return $basename; |
44 | } |
45 | |
059c085b |
46 | no Moose; |
146554c5 |
47 | |
965f3e35 |
48 | =for stopwords uploadtmp |
49 | |
146554c5 |
50 | =head1 NAME |
51 | |
bab3a22c |
52 | Catalyst::Request::Upload - handles file upload requests |
146554c5 |
53 | |
54 | =head1 SYNOPSIS |
55 | |
33455c7e |
56 | my $upload = $c->req->upload('field'); |
57 | |
3e22baa5 |
58 | $upload->basename; |
bab3a22c |
59 | $upload->copy_to; |
60 | $upload->fh; |
146554c5 |
61 | $upload->filename; |
4be535b1 |
62 | $upload->headers; |
3ffaf022 |
63 | $upload->link_to; |
146554c5 |
64 | $upload->size; |
32d4bba8 |
65 | $upload->slurp; |
146554c5 |
66 | $upload->tempname; |
67 | $upload->type; |
68 | |
7257e9db |
69 | To specify where Catalyst should put the temporary files, set the 'uploadtmp' |
70 | option in the Catalyst config. If unset, Catalyst will use the system temp dir. |
71 | |
72 | __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' ); |
73 | |
146554c5 |
74 | See also L<Catalyst>. |
75 | |
76 | =head1 DESCRIPTION |
77 | |
bab3a22c |
78 | This class provides accessors and methods to handle client upload requests. |
146554c5 |
79 | |
80 | =head1 METHODS |
81 | |
b5ecfcf0 |
82 | =head2 $upload->new |
cd3bb248 |
83 | |
bab3a22c |
84 | Simple constructor. |
cd3bb248 |
85 | |
b5ecfcf0 |
86 | =head2 $upload->copy_to |
47ae6960 |
87 | |
bab3a22c |
88 | Copies the temporary file using L<File::Copy>. Returns true for success, |
89 | false for failure. |
47ae6960 |
90 | |
3ffaf022 |
91 | $upload->copy_to('/path/to/target'); |
92 | |
47ae6960 |
93 | =cut |
94 | |
3ffaf022 |
95 | sub copy_to { |
c462faf0 |
96 | my $self = shift; |
97 | return File::Copy::copy( $self->tempname, @_ ); |
47ae6960 |
98 | } |
99 | |
b5ecfcf0 |
100 | =head2 $upload->fh |
146554c5 |
101 | |
bab3a22c |
102 | Opens a temporary file (see tempname below) and returns an L<IO::File> handle. |
146554c5 |
103 | |
b5ecfcf0 |
104 | =head2 $upload->filename |
146554c5 |
105 | |
bab3a22c |
106 | Returns the client-supplied filename. |
146554c5 |
107 | |
b5ecfcf0 |
108 | =head2 $upload->headers |
4be535b1 |
109 | |
bab3a22c |
110 | Returns an L<HTTP::Headers> object for the request. |
4be535b1 |
111 | |
b5ecfcf0 |
112 | =head2 $upload->link_to |
146554c5 |
113 | |
b0ad47c1 |
114 | Creates a hard link to the temporary file. Returns true for success, |
bab3a22c |
115 | false for failure. |
146554c5 |
116 | |
3ffaf022 |
117 | $upload->link_to('/path/to/target'); |
146554c5 |
118 | |
119 | =cut |
120 | |
3ffaf022 |
121 | sub link_to { |
47ae6960 |
122 | my ( $self, $target ) = @_; |
5c0ff128 |
123 | return CORE::link( $self->tempname, $target ); |
146554c5 |
124 | } |
125 | |
b5ecfcf0 |
126 | =head2 $upload->size |
146554c5 |
127 | |
bab3a22c |
128 | Returns the size of the uploaded file in bytes. |
146554c5 |
129 | |
b5ecfcf0 |
130 | =head2 $upload->slurp |
32d4bba8 |
131 | |
bab3a22c |
132 | Returns a scalar containing the contents of the temporary file. |
32d4bba8 |
133 | |
965f3e35 |
134 | Note that this will cause the filehandle pointed to by C<< $upload->fh >> to |
135 | be reset to the start of the file using seek and the file handle to be put |
136 | into binary mode. |
0fd00e7b |
137 | |
32d4bba8 |
138 | =cut |
139 | |
140 | sub slurp { |
141 | my ( $self, $layer ) = @_; |
142 | |
4be535b1 |
143 | unless ($layer) { |
32d4bba8 |
144 | $layer = ':raw'; |
145 | } |
146 | |
147 | my $content = undef; |
148 | my $handle = $self->fh; |
149 | |
150 | binmode( $handle, $layer ); |
151 | |
89cb63ec |
152 | $handle->seek(0, IO::File::SEEK_SET); |
32d4bba8 |
153 | while ( $handle->sysread( my $buffer, 8192 ) ) { |
154 | $content .= $buffer; |
155 | } |
156 | |
89cb63ec |
157 | $handle->seek(0, IO::File::SEEK_SET); |
32d4bba8 |
158 | return $content; |
159 | } |
160 | |
3e22baa5 |
161 | =head2 $upload->basename |
162 | |
163 | Returns basename for C<filename>. |
164 | |
b5ecfcf0 |
165 | =head2 $upload->tempname |
146554c5 |
166 | |
bab3a22c |
167 | Returns the path to the temporary file. |
146554c5 |
168 | |
b5ecfcf0 |
169 | =head2 $upload->type |
146554c5 |
170 | |
bab3a22c |
171 | Returns the client-supplied Content-Type. |
146554c5 |
172 | |
059c085b |
173 | =head2 meta |
174 | |
175 | Provided by Moose |
176 | |
bab3a22c |
177 | =head1 AUTHORS |
146554c5 |
178 | |
2f381252 |
179 | Catalyst Contributors, see Catalyst.pm |
146554c5 |
180 | |
181 | =head1 COPYRIGHT |
182 | |
536bee89 |
183 | This library is free software. You can redistribute it and/or modify |
146554c5 |
184 | it under the same terms as Perl itself. |
185 | |
186 | =cut |
187 | |
e5ecd5bc |
188 | __PACKAGE__->meta->make_immutable; |
189 | |
146554c5 |
190 | 1; |