X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FRequest%2FUpload.pm;h=ff0b5dee23b67376b517d9eaf5e8658bb93bd44f;hb=a2f2cde95194a17fe2401ae58c92b5494bac599f;hp=867c0980a94047ec42d024b4b534516defe7834f;hpb=3ffaf022fdbd8d058e76df96b6b0f117bd4c0c85;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm index 867c098..ff0b5de 100644 --- a/lib/Catalyst/Request/Upload.pm +++ b/lib/Catalyst/Request/Upload.pm @@ -3,6 +3,7 @@ package Catalyst::Request::Upload; use strict; use base 'Class::Accessor::Fast'; +use Catalyst::Exception; use File::Copy (); use IO::File (); @@ -21,6 +22,7 @@ Catalyst::Request::Upload - Catalyst Request Upload Class $upload->filename; $upload->link_to; $upload->size; + $upload->slurp; $upload->tempname; $upload->type; @@ -35,6 +37,10 @@ to the upload data. =over 4 +=item $upload->new + +simple constructor. + =item $upload->copy_to Copies tempname using C. Returns true for success, false otherwise. @@ -44,8 +50,8 @@ Copies tempname using C. Returns true for success, false otherwise. =cut sub copy_to { - my ( $self, $target, $buffer ) = @_; - return File::Copy::copy( $self->tempname, $target, $buffer ); + my $self = shift; + return File::Copy::copy( $self->tempname, @_ ); } =item $upload->fh @@ -57,8 +63,16 @@ Opens tempname and returns a C handle. 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; } @@ -85,6 +99,31 @@ sub link_to { Contains size of the file in bytes. +=item $upload->slurp + +Returns a scalar containing contents of tempname. + +=cut + +sub slurp { + my ( $self, $layer ) = @_; + + unless ( $layer ) { + $layer = ':raw'; + } + + my $content = undef; + my $handle = $self->fh; + + binmode( $handle, $layer ); + + while ( $handle->sysread( my $buffer, 8192 ) ) { + $content .= $buffer; + } + + return $content; +} + =item $upload->tempname Contains path to the temporary spool file.