1 package Catalyst::Request::Upload;
4 use base 'Class::Accessor::Fast';
6 use Catalyst::Exception;
11 __PACKAGE__->mk_accessors(qw/filename headers size tempname type basename/);
13 sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
17 Catalyst::Request::Upload - handles file upload requests
32 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
33 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
35 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
41 This class provides accessors and methods to handle client upload requests.
49 =head2 $upload->copy_to
51 Copies the temporary file using L<File::Copy>. Returns true for success,
54 $upload->copy_to('/path/to/target');
60 return File::Copy::copy( $self->tempname, @_ );
65 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
72 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
74 unless ( defined $fh ) {
76 my $filename = $self->tempname;
78 Catalyst::Exception->throw(
79 message => qq/Can't open '$filename': '$!'/ );
85 =head2 $upload->filename
87 Returns the client-supplied filename.
89 =head2 $upload->headers
91 Returns an L<HTTP::Headers> object for the request.
93 =head2 $upload->link_to
95 Creates a hard link to the temporary file. Returns true for success,
98 $upload->link_to('/path/to/target');
103 my ( $self, $target ) = @_;
104 return CORE::link( $self->tempname, $target );
109 Returns the size of the uploaded file in bytes.
111 =head2 $upload->slurp
113 Returns a scalar containing the contents of the temporary file.
118 my ( $self, $layer ) = @_;
125 my $handle = $self->fh;
127 binmode( $handle, $layer );
129 while ( $handle->sysread( my $buffer, 8192 ) ) {
138 unless ( $self->{basename} ) {
139 my $basename = $self->filename;
140 $basename =~ s|\\|/|g;
141 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
142 $basename =~ s|[^\w\.-]+|_|g;
143 $self->{basename} = $basename;
146 return $self->{basename};
149 =head2 $upload->basename
151 Returns basename for C<filename>.
153 =head2 $upload->tempname
155 Returns the path to the temporary file.
159 Returns the client-supplied Content-Type.
163 Catalyst Contributors, see Catalyst.pm
167 This program is free software, you can redistribute it and/or modify
168 it under the same terms as Perl itself.