X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FRequest%2FUpload.pm;h=9a5f1c4e6b3969e61f9c4a0c60c33e9d050c87df;hb=0ba80bce27a56d366c8d44c254332dd83f9ba0f9;hp=43a7cff191bfded79a5611a98b41a1f4adeb3013;hpb=b549b04945e1756688a2e79d7b065fe201001b6a;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm index 43a7cff..9a5f1c4 100644 --- a/lib/Catalyst/Request/Upload.pm +++ b/lib/Catalyst/Request/Upload.pm @@ -3,22 +3,24 @@ package Catalyst::Request::Upload; use strict; use base 'Class::Accessor::Fast'; +use Catalyst::Exception; use File::Copy (); use IO::File (); -__PACKAGE__->mk_accessors(qw/filename size tempname type/); +__PACKAGE__->mk_accessors(qw/filename headers size tempname type/); sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) } =head1 NAME -Catalyst::Request::Upload - Catalyst Request Upload Class +Catalyst::Request::Upload - handles file upload requests =head1 SYNOPSIS - $upload->copy_to - $upload->fh + $upload->copy_to; + $upload->fh; $upload->filename; + $upload->headers; $upload->link_to; $upload->size; $upload->slurp; @@ -29,16 +31,18 @@ See also L. =head1 DESCRIPTION -This is the Catalyst Request Upload class, which provides a set of accessors -to the upload data. +This class provides accessors and methods to handle client upload requests. =head1 METHODS -=over 4 +=head2 $upload->new -=item $upload->copy_to +Simple constructor. -Copies tempname using C. Returns true for success, false otherwise. +=head2 $upload->copy_to + +Copies the temporary file using L. Returns true for success, +false for failure. $upload->copy_to('/path/to/target'); @@ -49,29 +53,40 @@ sub copy_to { return File::Copy::copy( $self->tempname, @_ ); } -=item $upload->fh +=head2 $upload->fh -Opens tempname and returns a C handle. +Opens a temporary file (see tempname below) and returns an L handle. =cut sub fh { my $self = shift; - my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY ) - or die( "Can't open ", $self->tempname, ": ", $! ); + my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY ); + + unless ( defined $fh ) { + + my $filename = $self->tempname; + + Catalyst::Exception->throw( + message => qq/Can't open '$filename': '$!'/ ); + } return $fh; } -=item $upload->filename +=head2 $upload->filename -Contains client supplied filename. +Returns the client-supplied filename. -=item $upload->link_to +=head2 $upload->headers -Creates a hard link to the tempname. Returns true for success, -false otherwise. +Returns an L object for the request. + +=head2 $upload->link_to + +Creates a hard link to the temporary file. Returns true for success, +false for failure. $upload->link_to('/path/to/target'); @@ -82,20 +97,20 @@ sub link_to { return CORE::link( $self->tempname, $target ); } -=item $upload->size +=head2 $upload->size -Contains size of the file in bytes. +Returns the size of the uploaded file in bytes. -=item $upload->slurp +=head2 $upload->slurp -Returns a scalar containing contents of tempname. +Returns a scalar containing the contents of the temporary file. =cut sub slurp { my ( $self, $layer ) = @_; - unless ( $layer ) { + unless ($layer) { $layer = ':raw'; } @@ -111,19 +126,18 @@ sub slurp { return $content; } -=item $upload->tempname +=head2 $upload->tempname -Contains path to the temporary spool file. +Returns the path to the temporary file. -=item $upload->type +=head2 $upload->type -Contains client supplied Content-Type. +Returns the client-supplied Content-Type. -=back - -=head1 AUTHOR +=head1 AUTHORS Sebastian Riedel, C + Christian Hansen, C =head1 COPYRIGHT