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