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' );
37 It is provided a way to have configurable temporary directory.
38 If there is no config uploadtmp, system temprary directory will used.
40 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
46 This class provides accessors and methods to handle client upload requests.
54 =head2 $upload->copy_to
56 Copies the temporary file using L<File::Copy>. Returns true for success,
59 $upload->copy_to('/path/to/target');
65 return File::Copy::copy( $self->tempname, @_ );
70 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
77 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
79 unless ( defined $fh ) {
81 my $filename = $self->tempname;
83 Catalyst::Exception->throw(
84 message => qq/Can't open '$filename': '$!'/ );
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.
168 Sebastian Riedel, C<sri@cpan.org>
170 Christian Hansen, C<ch@ngmedia.com>
174 This program is free software, you can redistribute it and/or modify
175 it under the same terms as Perl itself.