1 package Catalyst::Request::Upload;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
6 use Catalyst::Exception;
10 use namespace::clean -except => 'meta';
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');
17 has basename => (is => 'ro', lazy_build => 1);
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': '$!'/ );
39 my $basename = $self->filename;
40 $basename =~ s|\\|/|g;
41 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
42 $basename =~ s|[^\w\.-]+|_|g;
50 Catalyst::Request::Upload - handles file upload requests
54 my $upload = $c->req->upload('field');
67 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
68 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
70 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
76 This class provides accessors and methods to handle client upload requests.
84 =head2 $upload->copy_to
86 Copies the temporary file using L<File::Copy>. Returns true for success,
89 $upload->copy_to('/path/to/target');
95 return File::Copy::copy( $self->tempname, @_ );
100 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
102 =head2 $upload->filename
104 Returns the client-supplied filename.
106 =head2 $upload->headers
108 Returns an L<HTTP::Headers> object for the request.
110 =head2 $upload->link_to
112 Creates a hard link to the temporary file. Returns true for success,
115 $upload->link_to('/path/to/target');
120 my ( $self, $target ) = @_;
121 return CORE::link( $self->tempname, $target );
126 Returns the size of the uploaded file in bytes.
128 =head2 $upload->slurp
130 Returns a scalar containing the contents of the temporary file.
132 Note that this method will cause the filehandle pointed to by
133 C<< $upload->fh >> to be seeked to the start of the file,
134 and the file handle to be put into binary mode.
139 my ( $self, $layer ) = @_;
146 my $handle = $self->fh;
148 binmode( $handle, $layer );
150 $handle->seek(0, IO::File::SEEK_SET);
151 while ( $handle->sysread( my $buffer, 8192 ) ) {
155 $handle->seek(0, IO::File::SEEK_SET);
159 =head2 $upload->basename
161 Returns basename for C<filename>.
163 =head2 $upload->tempname
165 Returns the path to the temporary file.
169 Returns the client-supplied Content-Type.
177 Catalyst Contributors, see Catalyst.pm
181 This library is free software. You can redistribute it and/or modify
182 it under the same terms as Perl itself.
186 __PACKAGE__->meta->make_immutable;