added a copy method to C::R::Upload and updated Cookbook
Christian Hansen [Mon, 11 Apr 2005 07:30:26 +0000 (07:30 +0000)]
lib/Catalyst/Manual/Cookbook.pod
lib/Catalyst/Request/Upload.pm

index 7f4a2db..aa38da5 100644 (file)
@@ -91,18 +91,13 @@ Catalyst Controller module 'upload' action:
         if ( $c->request->parameters->{form_submit} eq 'yes' ) {
 
             if ( my $upload = $c->request->upload('my_file') ) {
-
+            
                 my $filename = $upload->filename;
-                my $fh       = $upload->fh;
-
-                open( NEW_FILE, ">/tmp/upload/$filename" ) 
-                  or die( "Can't open file for writing: $!" );
-
-                while ( $fh->read( my $buf, 32768 ) ) {
-                    print NEW_FILE $buf;
+                my $target   = "/tmp/upload/$filename";
+                
+                unless ( $upload->link($target) || $upload->copy($target) ) {
+                    die( "Failed to copy '$filename' to '$target': $!" );
                 }
-
-                close(NEW_FILE);
             }
         }
         
@@ -133,18 +128,12 @@ Controller:
 
             for my $field ( $c->req->upload ) {
 
-                my $upload   = $c->request->upload($field);
                 my $filename = $upload->filename;
-                my $fh       = $upload->fh;
-
-                open( NEW_FILE, ">/tmp/upload/$filename" ) 
-                  or die ("Can't open file for writing: $!");
-
-                while ( $fh->read( my $buf, 32768 ) ) {
-                    print NEW_FILE $buf;
+                my $target   = "/tmp/upload/$filename";
+                
+                unless ( $upload->link($target) || $upload->copy($target) ) {
+                    die( "Failed to copy '$filename' to '$target': $!" );
                 }
-
-                close(NEW_FILE);
             }
         }
 
index ee1e5e2..389f810 100644 (file)
@@ -3,7 +3,8 @@ 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/);
 
@@ -15,6 +16,7 @@ Catalyst::Request::Upload - Catalyst Request Upload Class
 
 =head1 SYNOPSIS
 
+    $upload->copy
     $upload->fh
     $upload->filename;
     $upload->link;
@@ -26,13 +28,24 @@ 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->copy( $target [, $bufferlen ] )
+
+Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
+
+=cut
+
+sub copy {
+    my ( $self, $target, $buffer ) = @_;
+    return File::Copy::copy( $self->tempname, $target, $buffer );
+}
+
 =item $upload->fh
 
 Opens tempname and returns a C<IO::File> handle.
@@ -42,7 +55,7 @@ Opens tempname and returns a C<IO::File> handle.
 sub fh {
     my $self = shift;
 
-    my $fh = IO::File->new( $self->tempname, O_RDONLY )
+    my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY )
       or die( "Can't open ", $self->tempname, ": ", $! );
 
     return $fh;
@@ -62,9 +75,7 @@ success, false otherwise.
 =cut
 
 sub link {
-    my $self   = shift;
-    my $target = shift;
-
+    my ( $self, $target ) = @_;
     return CORE::link( $self->tempname, $target );
 }