1 package Catalyst::Request::Upload;
4 use base 'Class::Accessor::Fast';
6 use Catalyst::Exception;
10 __PACKAGE__->mk_accessors(qw/filename headers size tempname type/);
12 sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
16 Catalyst::Request::Upload - handles file upload requests
30 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
31 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
33 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
39 This class provides accessors and methods to handle client upload requests.
47 =head2 $upload->copy_to
49 Copies the temporary file using L<File::Copy>. Returns true for success,
52 $upload->copy_to('/path/to/target');
58 return File::Copy::copy( $self->tempname, @_ );
63 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
70 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
72 unless ( defined $fh ) {
74 my $filename = $self->tempname;
76 Catalyst::Exception->throw(
77 message => qq/Can't open '$filename': '$!'/ );
83 =head2 $upload->filename
85 Returns the client-supplied filename.
87 =head2 $upload->headers
89 Returns an L<HTTP::Headers> object for the request.
91 =head2 $upload->link_to
93 Creates a hard link to the temporary file. Returns true for success,
96 $upload->link_to('/path/to/target');
101 my ( $self, $target ) = @_;
102 return CORE::link( $self->tempname, $target );
107 Returns the size of the uploaded file in bytes.
109 =head2 $upload->slurp
111 Returns a scalar containing the contents of the temporary file.
116 my ( $self, $layer ) = @_;
123 my $handle = $self->fh;
125 binmode( $handle, $layer );
127 while ( $handle->sysread( my $buffer, 8192 ) ) {
134 =head2 $upload->tempname
136 Returns the path to the temporary file.
140 Returns the client-supplied Content-Type.
144 Sebastian Riedel, C<sri@cpan.org>
146 Christian Hansen, C<ch@ngmedia.com>
150 This program is free software, you can redistribute it and/or modify
151 it under the same terms as Perl itself.