Added Catalyst::Request::Upload
Christian Hansen [Sun, 10 Apr 2005 01:29:07 +0000 (01:29 +0000)]
lib/Catalyst/Engine.pm
lib/Catalyst/Engine/Apache/MP1.pm
lib/Catalyst/Engine/Apache/MP2.pm
lib/Catalyst/Engine/CGI.pm
lib/Catalyst/Engine/Test.pm
lib/Catalyst/Request.pm
lib/Catalyst/Request/Upload.pm [new file with mode: 0644]

index 8e0b513..95f7058 100644 (file)
@@ -14,6 +14,7 @@ use Text::ASCIITable::Wrap 'wrap';
 use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
 use Catalyst::Request;
+use Catalyst::Request::Upload;
 use Catalyst::Response;
 
 require Module::Pluggable::Fast;
index 7405269..6966e9e 100644 (file)
@@ -73,15 +73,14 @@ sub prepare_uploads {
 
     for my $upload ( $c->apache->upload ) {
 
-        my $hash = {
-            fh       => $upload->fh,
+        my $object = Catalyst::Request::Upload->new(
             filename => $upload->filename,
             size     => $upload->size,
             tempname => $upload->tempname,
             type     => $upload->type
-        };
+        );
 
-        push( @uploads, $upload->name, $hash );
+        push( @uploads, $upload->name, $object );
     }
 
     $c->req->_assign_values( $c->req->uploads, \@uploads );
index fe88555..43c9b09 100644 (file)
@@ -80,15 +80,14 @@ sub prepare_uploads {
 
         for my $upload ( $c->apache->upload($field) ) {
 
-            my $hash = {
-                fh       => $upload->fh,
+            my $object = Catalyst::Request::Upload->new(
                 filename => $upload->filename,
                 size     => $upload->size,
                 tempname => $upload->tempname,
                 type     => $upload->type
-            };
+            );
 
-            push( @uploads, $field, $hash );
+            push( @uploads, $field, $object );
         }
     }
 
index af26d08..936a38c 100644 (file)
@@ -189,13 +189,12 @@ sub prepare_uploads {
             my $disposition = $info->{'Content-Disposition'};
             my $filename    = ( $disposition =~ / filename="([^;]*)"/ )[0];
 
-            my $upload = {
-                fh       => $fh,
+            my $upload = Catalyst::Request::Upload->new(
                 filename => $filename,
                 size     => $size,
                 tempname => $tempname,
                 type     => $type
-            };
+            );
             
             push( @uploads, $param, $upload );
         }
index 0561348..102c8c9 100644 (file)
@@ -120,17 +120,17 @@ sub prepare_parameters {
 
             if ( $parameters{filename} ) {
 
-                my $fh = File::Temp->new;
+                my $fh = File::Temp->new( UNLINK => 0 );
                 $fh->write( $part->content ) or die $!;
-                seek( $fh, 0, 0 ) or die $!;
 
-                my $upload = {
-                    fh       => $fh,
+                my $upload = Catalyst::Request::Upload->new(
                     filename => $parameters{filename},
                     size     => ( stat $fh )[7],
                     tempname => $fh->filename,
                     type     => $part->content_type
-                };
+                );
+                
+                $fh->close;
 
                 push( @uploads, $parameters{name}, $upload );
                 push( @params,  $parameters{name}, $fh );
index f076fbd..22516e1 100644 (file)
@@ -18,11 +18,10 @@ sub header           { shift->headers->header(@_)           }
 sub referer          { shift->headers->referer(@_)          }
 sub user_agent       { shift->headers->user_agent(@_)       }
 
-
 sub _assign_values {
     my ( $self, $map, $values ) = @_;
-    
-    while ( my ( $name, $value ) = splice( @{ $values }, 0, 2 ) ) {
+
+    while ( my ( $name, $value ) = splice( @{$values}, 0, 2 ) ) {
 
         if ( exists $map->{$name} ) {
             for ( $map->{$name} ) {
@@ -226,7 +225,7 @@ A convenient method to $req->uploads.
     @fields  = $c->request->upload;
     
     for my $upload ( $c->request->upload('field') ) {
-        print $upload->{filename};
+        print $upload->filename;
     }
 
 =cut
@@ -258,38 +257,12 @@ sub upload {
 
 =item $req->uploads
 
-Returns a reference to a hash containing uploads. Values can
-be either a hashref or a arrayref containing hashrefs.
+Returns a reference to a hash containing uploads. Values can be either a 
+hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
 
     my $upload = $c->request->uploads->{field};
     my $upload = $c->request->uploads->{field}->[0];
 
-The upload hashref contains the following keys:
-
-=over 4
-
-=item * fh 
-
-Filehandle.
-
-=item * filename 
-
-Client supplied filename.
-
-=item * size
-
-Size of the file in bytes.
-
-=item * tempname
-
-Path to the temporary spool file.
-
-=item * type
-
-Client supplied Content-Type.
-
-=back
-
 =item $req->user_agent
 
 Shortcut to $req->headers->user_agent. User Agent version string.
diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm
new file mode 100644 (file)
index 0000000..21646d7
--- /dev/null
@@ -0,0 +1,97 @@
+package Catalyst::Request::Upload;
+
+use strict;
+use base 'Class::Accessor::Fast';
+
+use IO::File;
+
+__PACKAGE__->mk_accessors(qw/filename size tempname type/);
+
+sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
+
+=head1 NAME
+
+Catalyst::Request::Upload - Catalyst Request Upload Class
+
+=head1 SYNOPSIS
+
+    $upload->fh
+    $upload->filename;
+    $upload->link;
+    $upload->size;
+    $upload->tempname;
+    $upload->type;
+
+See also L<Catalyst>.
+
+=head1 DESCRIPTION
+
+This is the Catalyst Request Upload class, which provides a set of accessors to the
+upload data.
+
+=head1 METHODS
+
+=over 4
+
+=item $upload->fh
+
+Opens tempname and returns a C<IO::File> handle.
+
+=cut
+
+sub fh {
+    my $self = shift;
+
+    my $fh = IO::File->new( $self->tempname, O_RDWR )
+      or die( "Can't open ", $self->tempname, ": ", $! );
+
+    return $fh;
+}
+
+=item $upload->filename
+
+Contains client supplied filename.
+
+=item $upload->link
+
+Creates a new filename linked to the old filename.  Returns true for
+success, false otherwise.
+
+    $upload->link('/my/path');
+
+=cut
+
+sub link {
+    my $self   = shift;
+    my $target = shift;
+
+    return link( $self->tempname, $target );
+}
+
+=item $upload->size
+
+Contains size of the file in bytes.
+
+=item $upload->tempname
+
+Contains path to the temporary spool file.
+
+=item $upload->type
+
+Contains client supplied Content-Type.
+
+=back
+
+=head1 AUTHOR
+
+Sebastian Riedel, C<sri@cpan.org>
+Christian Hansen, C<ch@ngmedia.com>
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;