CGI-Easy-SendFile

 view release on metacpan or  search on metacpan

lib/CGI/Easy/SendFile.pm  view on Meta::CPAN

                $end    = $to;
            }
            elsif ($from ne q{} && $to eq q{} && $from < $len) {            # 0-, 500-, 999-
                $start  = $from;
            }
            elsif ($from eq q{} && $to ne q{} && 0 < $to && $to <= $len) {  # -1, -500, -1000
                $start  = $len - $to;
            }
        }
    }
    return ($start, $end);
}

sub _read_block {
    my ($file, $start, $size) = @_;
    my $data = q{};
    open my $fh, '<', $file or croak "open: $!";
    seek $fh, $start, 0;
    my ($n, $buf);
    while ($n = read $fh, $buf, min($size, BUF_SIZE)) {
        $size -= length $buf;
        $data .= $buf;
    }
    croak "read: $!" if !defined $n;
    close $fh or croak "close: $!";
    return \$data;
}


1; # Magic true value required at end of module
__END__

=encoding utf8

=head1 NAME

CGI::Easy::SendFile - send files from CGI to browser


=head1 VERSION

This document describes CGI::Easy::SendFile version v2.0.1


=head1 SYNOPSIS

    use CGI::Easy::SendFile qw( send_file );

    my $r = CGI::Easy::Request->new();
    my $h = CGI::Easy::Headers->new();

    my $data = send_file($r, $h, '/path/file.zip');
    print $h->compose();
    print ${$data};

    # -- send "file" generated in memory instead of real file
    my $dynamic_file = '…some binary data…';
    my $data = send_file($r, $h, \$dynamic_file);

    # -- simulate static image served by web server 
    #    (without "download file" dialog popup in browser)
    my $data = send_file($r, $h, 'avatar.png', {
            type    => 'image/png',
            cache   => 1,
            inline  => 1,
    });


=head1 DESCRIPTION

This module provide single function, which helps you prepare CGI reply for
sending file to browser.


=head1 EXPORTS

Nothing by default, but all documented functions can be explicitly imported.


=head1 INTERFACE 

=head2 send_file

    $data = send_file( $r, $h, '/path/file.zip' );
    $data = send_file( $r, $h, '/path/file.zip', \%opt );
    $data = send_file( $r, $h, \$dynamic_file );
    $data = send_file( $r, $h, \$dynamic_file, \%opt );

Prepare HTTP headers and content for CGI reply to send file.

    $r      CGI::Easy::Request object
    $h      CGI::Easy::Headers object
    $file   STRING (file name) or SCALARREF (file contents)
    %opt
      {type}    STRING (default "application/x-download")
      {range}   BOOL (default TRUE if $file is STRING,
                              FALSE if $file is SCALARREF)
      {cache}   BOOL (default FALSE)
      {inline}  BOOL (default FALSE)

=over

=item {type}

Custom value for 'Content-Type' header. These are equivalents:

    $data = send_file($r, $h, $file, {type=>'image/png'});

    $data = send_file($r, $h, $file);
    $h->{'Content-Type'} = 'image/png';

=item {range}

Enable/disable support for sending partial file contents, if requested
(this is usually used by file downloader applications to fetch files
faster using several simultaneous connections to download different file
parts). You shouldn't enable this option for dynamic files generated by
your CGI if contents of these files may differ for different CGI requests
sent by same user to same url.

If your web server configured to gzip CGI replies, it will disable this



( run in 0.902 second using v1.01-cache-2.11-cpan-364913b4093 )