1 package Catalyst::Request::Upload;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
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 => 'ro', lazy_build => 1);
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': '$!'/ );
38 my $basename = $self->filename;
39 $basename =~ s|\\|/|g;
40 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
41 $basename =~ s|[^\w\.-]+|_|g;
49 Catalyst::Request::Upload - handles file upload requests
64 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
65 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
67 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
73 This class provides accessors and methods to handle client upload requests.
81 =head2 $upload->copy_to
83 Copies the temporary file using L<File::Copy>. Returns true for success,
86 $upload->copy_to('/path/to/target');
92 return File::Copy::copy( $self->tempname, @_ );
97 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
99 =head2 $upload->filename
101 Returns the client-supplied filename.
103 =head2 $upload->headers
105 Returns an L<HTTP::Headers> object for the request.
107 =head2 $upload->link_to
109 Creates a hard link to the temporary file. Returns true for success,
112 $upload->link_to('/path/to/target');
117 my ( $self, $target ) = @_;
118 return CORE::link( $self->tempname, $target );
123 Returns the size of the uploaded file in bytes.
125 =head2 $upload->slurp
127 Returns a scalar containing the contents of the temporary file.
132 my ( $self, $layer ) = @_;
139 my $handle = $self->fh;
141 binmode( $handle, $layer );
143 while ( $handle->sysread( my $buffer, 8192 ) ) {
150 =head2 $upload->basename
152 Returns basename for C<filename>.
154 =head2 $upload->tempname
156 Returns the path to the temporary file.
160 Returns the client-supplied Content-Type.
168 Catalyst Contributors, see Catalyst.pm
172 This program is free software, you can redistribute it and/or modify
173 it under the same terms as Perl itself.
177 __PACKAGE__->meta->make_immutable;