Jifty::Web::FileUpload - Describes an HTTP file upload


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::Web::FileUpload - Describes an HTTP file upload

DESCRIPTION

Top

Currently this module is very much geared towards the use case of the current request offering a file upload, and inspecting CGI to produce metadata.

Refactorings to eliminate these assumptions are very welcome.

new PARAMHASH

Creates a new file upload object. The possible arguments in the PARAMHASH are:

filehandle

The filehandle to read the content from. If this is not an Fh object produced by CGI, then content_type is mandatory and you probably want to set filename yourself.

content (optional)

The upload's content. Will be intuited if possible.

filename (optional)

The upload's filename as given by the client (i.e. ¬ on disk). Will be intuited if possible.

content_type (optional)

The content type as reported by the client.

content

Lazily slurps in the filehandle's content.

new_from_fh Fh

Convenience method, since the other bits can be gleaned from the Fh object.

new_from_plack $u


Jifty documentation Contained in the Jifty distribution.
package Jifty::Web::FileUpload;
use strict;
use warnings;
use base qw/Jifty::Object Class::Accessor::Fast/;

__PACKAGE__->mk_accessors(qw(filehandle _content filename content_type));

use overload (
    q{""} => sub { $_[0]->filename },
    '*{}' => sub { $_[0]->filehandle },
    '${}' => sub { \($_[0]->filename) },
    fallback => 1,
);

sub new {
    my $class = shift;
    my $self = bless {}, $class;

    my %args = (
        filehandle   => undef,
        content      => undef,
        filename     => undef,
        content_type => undef,
        @_,
    );

    my $fh = $args{filehandle};

    if (!defined($args{filename})) {
        $args{filename} = "$fh";

        # Strip all but the basename for consistency between browsers
        $args{filename} =~ s#^.*[\\/]##;
    }

    if (!defined($args{content_type})) {
        ref($fh) eq 'Fh'
            or die "The filehandle must be an Fh object produced by CGI";

        my $info = Jifty->handler->cgi->uploadInfo($fh);
        $args{content_type} = $info->{'Content-Type'}
            if defined $info;
    }

    $self->filehandle($fh);
    $self->content($args{content});
    $self->filename($args{filename});
    $self->content_type($args{content_type});
    return $self;
}

sub content {
    my $self = shift;
    if (@_) {
        return $self->_content(@_);
    }

    my $content = $self->_content;
    if (!defined($content)) {
        my $fh = $self->filehandle;;
        local $/;
        $content = <$fh>;
        $self->_content($content);
    }

    return $content;
}

# DEPRECATED
sub new_from_fh {
    my $self = shift;
    my $fh   = shift;

    $self->new(filehandle => $fh);
}

sub new_from_plack {
    my $self = shift;
    my $u    = shift;

    open my $fh, '<:raw', $u->tempname
        or die "Can't open '@{[ $u->tempname ]}': '$!'";

    $self->new(filehandle   => $fh,
               filename     => $u->basename,
               content_type => $u->type,
           );
}

1;