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;
48 =for stopwords uploadtmp
52 Catalyst::Request::Upload - handles file upload requests
56 my $upload = $c->req->upload('field');
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.
72 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
78 This class provides accessors and methods to handle client upload requests.
86 =head2 $upload->copy_to
88 Copies the temporary file using L<File::Copy>. Returns true for success,
91 $upload->copy_to('/path/to/target');
97 return File::Copy::copy( $self->tempname, @_ );
102 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
104 =head2 $upload->filename
106 Returns the client-supplied filename.
108 =head2 $upload->headers
110 Returns an L<HTTP::Headers> object for the request.
112 =head2 $upload->link_to
114 Creates a hard link to the temporary file. Returns true for success,
117 $upload->link_to('/path/to/target');
122 my ( $self, $target ) = @_;
123 return CORE::link( $self->tempname, $target );
128 Returns the size of the uploaded file in bytes.
130 =head2 $upload->slurp
132 Returns a scalar containing the contents of the temporary file.
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
141 my ( $self, $layer ) = @_;
148 my $handle = $self->fh;
150 binmode( $handle, $layer );
152 $handle->seek(0, IO::File::SEEK_SET);
153 while ( $handle->sysread( my $buffer, 8192 ) ) {
157 $handle->seek(0, IO::File::SEEK_SET);
161 =head2 $upload->basename
163 Returns basename for C<filename>.
165 =head2 $upload->tempname
167 Returns the path to the temporary file.
171 Returns the client-supplied Content-Type.
179 Catalyst Contributors, see Catalyst.pm
183 This library is free software. You can redistribute it and/or modify
184 it under the same terms as Perl itself.
188 __PACKAGE__->meta->make_immutable;