From: Christian Hansen Date: Sun, 10 Apr 2005 01:29:07 +0000 (+0000) Subject: Added Catalyst::Request::Upload X-Git-Tag: 5.7099_04~1585 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=146554c575f7f9fda102985196e0b6b364847bc0 Added Catalyst::Request::Upload --- diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 8e0b513..95f7058 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -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; diff --git a/lib/Catalyst/Engine/Apache/MP1.pm b/lib/Catalyst/Engine/Apache/MP1.pm index 7405269..6966e9e 100644 --- a/lib/Catalyst/Engine/Apache/MP1.pm +++ b/lib/Catalyst/Engine/Apache/MP1.pm @@ -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 ); diff --git a/lib/Catalyst/Engine/Apache/MP2.pm b/lib/Catalyst/Engine/Apache/MP2.pm index fe88555..43c9b09 100644 --- a/lib/Catalyst/Engine/Apache/MP2.pm +++ b/lib/Catalyst/Engine/Apache/MP2.pm @@ -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 ); } } diff --git a/lib/Catalyst/Engine/CGI.pm b/lib/Catalyst/Engine/CGI.pm index af26d08..936a38c 100644 --- a/lib/Catalyst/Engine/CGI.pm +++ b/lib/Catalyst/Engine/CGI.pm @@ -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 ); } diff --git a/lib/Catalyst/Engine/Test.pm b/lib/Catalyst/Engine/Test.pm index 0561348..102c8c9 100644 --- a/lib/Catalyst/Engine/Test.pm +++ b/lib/Catalyst/Engine/Test.pm @@ -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 ); diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index f076fbd..22516e1 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -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 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 index 0000000..21646d7 --- /dev/null +++ b/lib/Catalyst/Request/Upload.pm @@ -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. + +=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 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 +Christian Hansen, C + +=head1 COPYRIGHT + +This program is free software, you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +1;