1 package Catalyst::Request::Upload;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
6 use Catalyst::Exception;
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');
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': '$!'/ );
40 Catalyst::Request::Upload - handles file upload requests
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.
58 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
64 This class provides accessors and methods to handle client upload requests.
72 =head2 $upload->copy_to
74 Copies the temporary file using L<File::Copy>. Returns true for success,
77 $upload->copy_to('/path/to/target');
83 return File::Copy::copy( $self->tempname, @_ );
88 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
90 =head2 $upload->filename
92 Returns the client-supplied filename.
94 =head2 $upload->headers
96 Returns an L<HTTP::Headers> object for the request.
98 =head2 $upload->link_to
100 Creates a hard link to the temporary file. Returns true for success,
103 $upload->link_to('/path/to/target');
108 my ( $self, $target ) = @_;
109 return CORE::link( $self->tempname, $target );
114 Returns the size of the uploaded file in bytes.
116 =head2 $upload->slurp
118 Returns a scalar containing the contents of the temporary file.
123 my ( $self, $layer ) = @_;
130 my $handle = $self->fh;
132 binmode( $handle, $layer );
134 while ( $handle->sysread( my $buffer, 8192 ) ) {
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;
151 return $self->{basename};
154 =head2 $upload->basename
156 Returns basename for C<filename>.
158 =head2 $upload->tempname
160 Returns the path to the temporary file.
164 Returns the client-supplied Content-Type.
172 Catalyst Contributors, see Catalyst.pm
176 This program is free software, you can redistribute it and/or modify
177 it under the same terms as Perl itself.
181 __PACKAGE__->meta->make_immutable;