1 package Catalyst::Request::Upload;
5 use Catalyst::Exception;
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');
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': '$!'/ );
39 Catalyst::Request::Upload - handles file upload requests
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.
57 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
63 This class provides accessors and methods to handle client upload requests.
71 =head2 $upload->copy_to
73 Copies the temporary file using L<File::Copy>. Returns true for success,
76 $upload->copy_to('/path/to/target');
82 return File::Copy::copy( $self->tempname, @_ );
87 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
89 =head2 $upload->filename
91 Returns the client-supplied filename.
93 =head2 $upload->headers
95 Returns an L<HTTP::Headers> object for the request.
97 =head2 $upload->link_to
99 Creates a hard link to the temporary file. Returns true for success,
102 $upload->link_to('/path/to/target');
107 my ( $self, $target ) = @_;
108 return CORE::link( $self->tempname, $target );
113 Returns the size of the uploaded file in bytes.
115 =head2 $upload->slurp
117 Returns a scalar containing the contents of the temporary file.
122 my ( $self, $layer ) = @_;
129 my $handle = $self->fh;
131 binmode( $handle, $layer );
133 while ( $handle->sysread( my $buffer, 8192 ) ) {
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;
150 return $self->{basename};
153 =head2 $upload->basename
155 Returns basename for C<filename>.
157 =head2 $upload->tempname
159 Returns the path to the temporary file.
163 Returns the client-supplied Content-Type.
171 Catalyst Contributors, see Catalyst.pm
175 This program is free software, you can redistribute it and/or modify
176 it under the same terms as Perl itself.
180 __PACKAGE__->meta->make_immutable;