1 package Catalyst::Request::Upload;
5 use Catalyst::Exception;
12 has filename => (is => 'rw');
13 has headers => (is => 'rw');
14 has size => (is => 'rw');
15 has tempname => (is => 'rw');
16 has type => (is => 'rw');
17 has basename => (is => 'rw');
26 my $fh = IO::File->new($self->tempname, IO::File::O_RDONLY);
27 unless ( defined $fh ) {
28 my $filename = $self->tempname;
29 Catalyst::Exception->throw(
30 message => qq/Can't open '$filename': '$!'/ );
41 Catalyst::Request::Upload - handles file upload requests
56 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
57 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
59 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
61 It is provided a way to have configurable temporary directory.
62 If there is no config uploadtmp, system temprary directory will used.
64 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
70 This class provides accessors and methods to handle client upload requests.
78 =head2 $upload->copy_to
80 Copies the temporary file using L<File::Copy>. Returns true for success,
83 $upload->copy_to('/path/to/target');
89 return File::Copy::copy( $self->tempname, @_ );
94 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
96 =head2 $upload->filename
98 Returns the client-supplied filename.
100 =head2 $upload->headers
102 Returns an L<HTTP::Headers> object for the request.
104 =head2 $upload->link_to
106 Creates a hard link to the temporary file. Returns true for success,
109 $upload->link_to('/path/to/target');
114 my ( $self, $target ) = @_;
115 return CORE::link( $self->tempname, $target );
120 Returns the size of the uploaded file in bytes.
122 =head2 $upload->slurp
124 Returns a scalar containing the contents of the temporary file.
129 my ( $self, $layer ) = @_;
136 my $handle = $self->fh;
138 binmode( $handle, $layer );
140 while ( $handle->sysread( my $buffer, 8192 ) ) {
149 unless ( $self->{basename} ) {
150 my $basename = $self->filename;
151 $basename =~ s|\\|/|g;
152 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
153 $basename =~ s|[^\w\.-]+|_|g;
154 $self->{basename} = $basename;
157 return $self->{basename};
160 =head2 $upload->basename
162 Returns basename for C<filename>.
164 =head2 $upload->tempname
166 Returns the path to the temporary file.
170 Returns the client-supplied Content-Type.
178 Sebastian Riedel, C<sri@cpan.org>
180 Christian Hansen, C<ch@ngmedia.com>
184 This program is free software, you can redistribute it and/or modify
185 it under the same terms as Perl itself.