NAME

Net::API::REST::Request::Upload - Apache2 Request Upload Object

SYNOPSIS

    use Net::API::REST::Request::Params;
    ## $r is the Apache2::RequestRec object
    my $req = Net::API::REST::Request::Params->new(
        request         => $r,
        # pool of 2Mb
        brigade_limit   => 2097152,
        disable_uploads => 0,
        # For example: 3Mb
        read_limit      => 3145728,
        temp_dir        => '/home/me/my/tmp'
        upload_hook     => sub
        {
            my( $upload, $new_data ) = @_; 
            # do something
        },
    );
    
    my $file = $req->upload( 'file_upload' );

    # or more simply
    use parent qw( Net::API::REST )
    
    # in your sub
    my $self = shift( @_ );
    my $file = $self->request->upload( 'file_upload' );
    # or
    my $file = $self->request->param( 'file_upload' );

    print( "No check done on data? ", $file->is_tainted ? 'no' : 'yes', "\n" );
    print( "Is it encoded in utf8? ", $file->charset == 8 ? 'yes' : 'no', "\n" );
    
    my $field_header = $file->info;
    
    # Returns the APR::Brigade object content for file_upload
    my $brigade = $field->bucket
    
    printf( "File name provided by client is: %s\n", $file->filename );
    
    # link to the temporary file or make a copy if on different file system
    $file->link( '/to/my/temp/file.png' );
    
    my $buff;
    # Read in our buffer if this is less than 500Kb
    $file->slurp( $buff ) if( $file->length < 512000 );
    
    print( "Uploaded data is %d bytes big\n, $file->length );
    
    print( "MIME type of uploaded data is: %s\n", $file->type );
    
    print( "Temporary file name is: %s\n", $file->tempname );
    
    my $io = $file->io;
    print while( $io->read( $_ ) );
    
    # overloaded object reverting to $file->value
    print( "Data is: $file\n" );
    
    print( "Data is: ", $file->value, "\n" );

VERSION

    v0.1.0

DESCRIPTION

This is a module to deal with data upload leveraging directly Apache mod_perl's methods making it fast and powerful.

METHODS

bucket APR::Brigade object

Get/set the APR::Brigade file-upload content for this param.

May also be called as upload

charset integer

Get/set the param's internal charset. The charset is a number between 0 and 255; the current recognized values are

0 APREQ_CHARSET_ASCII (7-bit us-ascii)

1 APREQ_CHARSET_LATIN1 (8-bit iso-8859-1)

2 APREQ_CHARSET_CP1252 (8-bit Windows-1252)

8 APREQ_CHARSET_UTF8 (utf8 encoded Unicode)

    my $charset = $up->charset;
    $up->charset( 8 );
    print( "Data in utf8 ? ", $up->charset == 8 ? 'yes' : 'no', "\n" );

filename string

Returns the client-side filename associated with this param.

Depending on the user agent, this may be the file full path name or just the file base name.

fh

Returns a seekable filehandle representing the file-upload content.

info APR::Table object

Get/set the APR::Table headers for this param.

    my $info = $up->info;
    while( my( $hdr_name, $hdr_value ) = each( %$info ) )
    {
        # etc
    }
    printf( "Content type is: %s\n", $up->info->{'Content-type'} );
    
    # could yield for example: application/json; charset=utf-8

See also "type", but be careful $up-info->{'Content-type'}> is not necessarily the same.

io

Returns an APR::Request::Brigade::IO object, which can be treated as a non-seekable IO stream.

This is more efficient than "fh"

This object has the read and readline methods corresponding to the methods READ and READLINE from APR::Request::Brigade

    $io->read( $buffer );

    # or
    $io->read( $buffer, $length );

    # or
    $io->read( $buffer, $length, $offset );

Reads data from the brigade $io into $buffer. When omitted $length defaults to "-1", which reads the first bucket into $buffer. A positive $length will read in $length bytes, or the remainder of the brigade, whichever is greater. $offset represents the index in $buffer to read the new data.

    $io->readline;

Returns the first line of data from the bride. Lines are terminated by linefeeds (the '\012' character), but this may be changed to $/ instead.

length integer

Returns the size of the param's file-upload content.

May also be called as size

Provided with a file path and this will link the file-upload content with the local file named $path. Creates a hard-link if the spoolfile's (see upload_tempname) temporary directory is on the same device as $path; otherwise this writes a copy.

This is useful to avoid recreating the data. This works on *nix-like systems

    my $up = $req->param( 'file_upload' );
    $up->link( '/to/my/location.png' ) ||
        die( sprintf( "Cannot symlink from %s: $!\n", $up->tempname ) );

make

Fast XS param constructor.

    Net::API::REST::Request::Param::Upload->make( $pool, $name, $value )

name string

Returns the param's name, i.e. the html form field name. This attribute cannot be modified.

slurp

Provided with a variable, such as $data and this reads the entire file-upload content into $data and returns an integer representing the size of $data.

    my $up = $req->param( 'file_upload' );
    my $size = $up->slurp( $data );

tempname string

Returns the name of the local spoolfile for this param.

type string

Returns the MIME-type of the param's file-upload content.

value string

Returns the param's value. This attribute cannot be modified.

AUTHOR

Jacques Deguest <jack@deguest.jp>

CPAN ID: jdeguest

https://git.deguest.jp/jack/Net-API-REST

SEE ALSO

Apache2::Request, APR::Request, APR::Request::Param, APR::Request::Apache2

COPYRIGHT & LICENSE

Copyright (c) 2018-2019 DEGUEST Pte. Ltd.

You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.