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