X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FRequest%2FUpload.pm;h=a31df3d5ad7e692c2597c11b93a31cf6e17ce8f7;hb=32d4bba8cb8276572093428eef50bf0bda59aa3d;hp=21646d78d90d743680e6163805a3a6cb3337a64c;hpb=146554c575f7f9fda102985196e0b6b364847bc0;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm index 21646d7..a31df3d 100644 --- a/lib/Catalyst/Request/Upload.pm +++ b/lib/Catalyst/Request/Upload.pm @@ -3,11 +3,11 @@ package Catalyst::Request::Upload; use strict; use base 'Class::Accessor::Fast'; -use IO::File; +use File::Copy (); +use IO::File (); __PACKAGE__->mk_accessors(qw/filename size tempname type/); -sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) } =head1 NAME @@ -15,10 +15,12 @@ Catalyst::Request::Upload - Catalyst Request Upload Class =head1 SYNOPSIS + $upload->copy_to $upload->fh $upload->filename; - $upload->link; + $upload->link_to; $upload->size; + $upload->slurp; $upload->tempname; $upload->type; @@ -26,13 +28,34 @@ See also L. =head1 DESCRIPTION -This is the Catalyst Request Upload class, which provides a set of accessors to the -upload data. +This is the Catalyst Request Upload class, which provides a set of accessors +to the upload data. =head1 METHODS =over 4 +=item $upload->new + +Constructor. Normally only for engine use. + +=cut + +sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) } + +=item $upload->copy_to + +Copies tempname using C. Returns true for success, false otherwise. + + $upload->copy_to('/path/to/target'); + +=cut + +sub copy_to { + my $self = shift; + return File::Copy::copy( $self->tempname, @_ ); +} + =item $upload->fh Opens tempname and returns a C handle. @@ -42,7 +65,7 @@ Opens tempname and returns a C handle. sub fh { my $self = shift; - my $fh = IO::File->new( $self->tempname, O_RDWR ) + my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY ) or die( "Can't open ", $self->tempname, ": ", $! ); return $fh; @@ -52,26 +75,49 @@ sub fh { Contains client supplied filename. -=item $upload->link +=item $upload->link_to -Creates a new filename linked to the old filename. Returns true for -success, false otherwise. +Creates a hard link to the tempname. Returns true for success, +false otherwise. - $upload->link('/my/path'); + $upload->link_to('/path/to/target'); =cut -sub link { - my $self = shift; - my $target = shift; - - return link( $self->tempname, $target ); +sub link_to { + my ( $self, $target ) = @_; + return CORE::link( $self->tempname, $target ); } =item $upload->size 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.