1 package Catalyst::Request::Upload;
6 use Catalyst::Exception;
11 has filename => (is => 'rw');
12 has headers => (is => 'rw');
13 has size => (is => 'rw');
14 has tempname => (is => 'rw');
15 has type => (is => 'rw');
16 has basename => (is => 'rw');
25 my $fh = IO::File->new($self->tempname, IO::File::O_RDONLY);
26 unless ( defined $fh ) {
27 my $filename = $self->tempname;
28 Catalyst::Exception->throw(
29 message => qq/Can't open '$filename': '$!'/ );
40 Catalyst::Request::Upload - handles file upload requests
55 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
56 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
58 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
60 It is provided a way to have configurable temporary directory.
61 If there is no config uploadtmp, system temprary directory will used.
63 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
69 This class provides accessors and methods to handle client upload requests.
77 =head2 $upload->copy_to
79 Copies the temporary file using L<File::Copy>. Returns true for success,
82 $upload->copy_to('/path/to/target');
88 return File::Copy::copy( $self->tempname, @_ );
93 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
95 =head2 $upload->filename
97 Returns the client-supplied filename.
99 =head2 $upload->headers
101 Returns an L<HTTP::Headers> object for the request.
103 =head2 $upload->link_to
105 Creates a hard link to the temporary file. Returns true for success,
108 $upload->link_to('/path/to/target');
113 my ( $self, $target ) = @_;
114 return CORE::link( $self->tempname, $target );
119 Returns the size of the uploaded file in bytes.
121 =head2 $upload->slurp
123 Returns a scalar containing the contents of the temporary file.
128 my ( $self, $layer ) = @_;
135 my $handle = $self->fh;
137 binmode( $handle, $layer );
139 while ( $handle->sysread( my $buffer, 8192 ) ) {
148 unless ( $self->{basename} ) {
149 my $basename = $self->filename;
150 $basename =~ s|\\|/|g;
151 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
152 $basename =~ s|[^\w\.-]+|_|g;
153 $self->{basename} = $basename;
156 return $self->{basename};
159 =head2 $upload->basename
161 Returns basename for C<filename>.
163 =head2 $upload->tempname
165 Returns the path to the temporary file.
169 Returns the client-supplied Content-Type.
177 Sebastian Riedel, C<sri@cpan.org>
179 Christian Hansen, C<ch@ngmedia.com>
183 This program is free software, you can redistribute it and/or modify
184 it under the same terms as Perl itself.
188 __PACKAGE__->meta->make_immutable;