use Tree::Simple;
use Tree::Simple::Visitor::FindByPath;
use Catalyst::Request;
+use Catalyst::Request::Upload;
use Catalyst::Response;
require Module::Pluggable::Fast;
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 );
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 );
}
}
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 );
}
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 );
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} ) {
@fields = $c->request->upload;
for my $upload ( $c->request->upload('field') ) {
- print $upload->{filename};
+ print $upload->filename;
}
=cut
=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.
--- /dev/null
+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;