X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FRequest%2FUpload.pm;h=25e6d89f58f52183f8c1d44666367a715c72b4c6;hb=74dafab798a163c251e09de7fcc21a267d1678a6;hp=5891332b49adc0df5cad009ff49763536155241f;hpb=c539a1c32cc974e531bea851e9f393800ed7a1c0;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm index 5891332..25e6d89 100644 --- a/lib/Catalyst/Request/Upload.pm +++ b/lib/Catalyst/Request/Upload.pm @@ -3,11 +3,13 @@ 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 @@ -18,8 +20,10 @@ Catalyst::Request::Upload - Catalyst Request Upload Class $upload->copy_to $upload->fh $upload->filename; + $upload->headers; $upload->link_to; $upload->size; + $upload->slurp; $upload->tempname; $upload->type; @@ -36,11 +40,7 @@ to the upload data. =item $upload->new -Constructor. Normally only for engine use. - -=cut - -sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) } +simple constructor. =item $upload->copy_to @@ -64,8 +64,15 @@ 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; } @@ -74,6 +81,10 @@ sub fh { Contains client supplied filename. +=item $upload->headers + +Returns a C object. + =item $upload->link_to Creates a hard link to the tempname. Returns true for success, @@ -92,6 +103,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.