1 package Catalyst::Request::Upload;
5 use Catalyst::Exception;
10 has filename => (is => 'rw');
11 has headers => (is => 'rw');
12 has size => (is => 'rw');
13 has tempname => (is => 'rw');
14 has type => (is => 'rw');
15 has basename => (is => 'rw');
24 my $fh = IO::File->new($self->tempname, IO::File::O_RDONLY);
25 unless ( defined $fh ) {
26 my $filename = $self->tempname;
27 Catalyst::Exception->throw(
28 message => qq/Can't open '$filename': '$!'/ );
39 Catalyst::Request::Upload - handles file upload requests
54 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
55 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
57 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
59 It is provided a way to have configurable temporary directory.
60 If there is no config uploadtmp, system temprary directory will used.
62 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
68 This class provides accessors and methods to handle client upload requests.
76 =head2 $upload->copy_to
78 Copies the temporary file using L<File::Copy>. Returns true for success,
81 $upload->copy_to('/path/to/target');
87 return File::Copy::copy( $self->tempname, @_ );
92 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
94 =head2 $upload->filename
96 Returns the client-supplied filename.
98 =head2 $upload->headers
100 Returns an L<HTTP::Headers> object for the request.
102 =head2 $upload->link_to
104 Creates a hard link to the temporary file. Returns true for success,
107 $upload->link_to('/path/to/target');
112 my ( $self, $target ) = @_;
113 return CORE::link( $self->tempname, $target );
118 Returns the size of the uploaded file in bytes.
120 =head2 $upload->slurp
122 Returns a scalar containing the contents of the temporary file.
127 my ( $self, $layer ) = @_;
134 my $handle = $self->fh;
136 binmode( $handle, $layer );
138 while ( $handle->sysread( my $buffer, 8192 ) ) {
147 unless ( $self->{basename} ) {
148 my $basename = $self->filename;
149 $basename =~ s|\\|/|g;
150 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
151 $basename =~ s|[^\w\.-]+|_|g;
152 $self->{basename} = $basename;
155 return $self->{basename};
158 =head2 $upload->basename
160 Returns basename for C<filename>.
162 =head2 $upload->tempname
164 Returns the path to the temporary file.
168 Returns the client-supplied Content-Type.
176 Sebastian Riedel, C<sri@cpan.org>
178 Christian Hansen, C<ch@ngmedia.com>
182 This program is free software, you can redistribute it and/or modify
183 it under the same terms as Perl itself.
187 __PACKAGE__->meta->make_immutable;