Added Catalyst::Exception
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
index 867c098..ff0b5de 100644 (file)
@@ -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<File::Copy>. Returns true for success, false otherwise.
@@ -44,8 +50,8 @@ Copies tempname using C<File::Copy>. 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<IO::File> 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.