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' );
35 It is provided a way to have configurable temporary directory.
36 If there is no config uploadtmp, system temprary directory will used.
38 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
44 This class provides accessors and methods to handle client upload requests.
52 =head2 $upload->copy_to
54 Copies the temporary file using L<File::Copy>. Returns true for success,
57 $upload->copy_to('/path/to/target');
63 return File::Copy::copy( $self->tempname, @_ );
68 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
75 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
77 unless ( defined $fh ) {
79 my $filename = $self->tempname;
81 Catalyst::Exception->throw(
82 message => qq/Can't open '$filename': '$!'/ );
88 =head2 $upload->filename
90 Returns the client-supplied filename.
92 =head2 $upload->headers
94 Returns an L<HTTP::Headers> object for the request.
96 =head2 $upload->link_to
98 Creates a hard link to the temporary file. Returns true for success,
101 $upload->link_to('/path/to/target');
106 my ( $self, $target ) = @_;
107 return CORE::link( $self->tempname, $target );
112 Returns the size of the uploaded file in bytes.
114 =head2 $upload->slurp
116 Returns a scalar containing the contents of the temporary file.
121 my ( $self, $layer ) = @_;
128 my $handle = $self->fh;
130 binmode( $handle, $layer );
132 while ( $handle->sysread( my $buffer, 8192 ) ) {
139 =head2 $upload->tempname
141 Returns the path to the temporary file.
145 Returns the client-supplied Content-Type.
149 Sebastian Riedel, C<sri@cpan.org>
151 Christian Hansen, C<ch@ngmedia.com>
155 This program is free software, you can redistribute it and/or modify
156 it under the same terms as Perl itself.