Missed some changes, merged again from r1019
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
index 2cd3eb9..25e6d89 100644 (file)
@@ -3,9 +3,11 @@ package Catalyst::Request::Upload;
 use strict;
 use base 'Class::Accessor::Fast';
 
-use IO::File;
+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] : {@_} ) }
 
@@ -15,10 +17,13 @@ Catalyst::Request::Upload - Catalyst Request Upload Class
 
 =head1 SYNOPSIS
 
+    $upload->copy_to
     $upload->fh
     $upload->filename;
-    $upload->link;
+    $upload->headers;
+    $upload->link_to;
     $upload->size;
+    $upload->slurp;
     $upload->tempname;
     $upload->type;
 
@@ -26,13 +31,30 @@ See also L<Catalyst>.
 
 =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
+
+simple constructor.
+
+=item $upload->copy_to
+
+Copies tempname using C<File::Copy>. 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<IO::File> handle.
@@ -42,8 +64,15 @@ Opens tempname and returns a C<IO::File> handle.
 sub fh {
     my $self = shift;
 
-    my $fh = IO::File->new( $self->tempname, 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;
 }
@@ -52,26 +81,53 @@ sub fh {
 
 Contains client supplied filename.
 
-=item $upload->link
+=item $upload->headers
 
-Creates a new filename linked to the old filename.  Returns true for
-success, false otherwise.
+Returns a C<HTTP::Headers> object.
 
-    $upload->link('/my/path');
+=item $upload->link_to
 
-=cut
+Creates a hard link to the tempname.  Returns true for success, 
+false otherwise.
 
-sub link {
-    my $self   = shift;
-    my $target = shift;
+    $upload->link_to('/path/to/target');
 
-    return link( $self->tempname, $target );
+=cut
+
+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.