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