Apache2-ASP
view release on metacpan or search on metacpan
lib/Apache2/ASP/MediaManager.pm view on Meta::CPAN
use strict;
use base 'Apache2::ASP::FormHandler';
use vars __PACKAGE__->VARS;
sub run {
my ($s, $context) = @_;
my $uploadID = $Form->{uploadID};
$Session->{"upload$uploadID" . "percent_complete"} ||= 0;
$Response->Expires( -30 );
$Response->Write( $Session->{"upload$uploadID" . "percent_complete"} );
}# end run()
1;# return true:
And add call out to it via AJAX - you can get real-time upload progress information
about the current upload.
Example:
window.setInterval(function() {
httpOb.open("GET", "/handlers/UploadProgress?uploadID=2kj4hkj234h", true);
httpOb.onreadystatechange = function() {
if( httpOb.readyState == 4 ) {
document.getElementById("percent_complete").innerHTML = httpOb.responseText + '%';
}// end if()
};
}, 1000);
You should also add an element with an id of "percent_complete" to he form:
<div id="percent_complete">0%</div>
=head1 PUBLIC PROPERTIES
None.
=head1 PUBLIC METHODS
Nothing you need to worry about.
=head1 EVENTS
lib/Apache2/ASP/UploadHandler.pm view on Meta::CPAN
#==============================================================================
sub upload_start
{
my ($s, $context, $Upload) = @_;
return;
return unless $Upload;
# Store the upload information in the Session for external retrieval:
$LastUpdate = time();
$LastPercent = $Upload->{percent_complete} || 0;
my $uploadID = $s->_args('uploadID');
$context->session->{"upload$uploadID$_"} = $Upload->{$_}
foreach grep { $_ !~ m/data/ } keys(%$Upload);
$context->session->save;
}# end upload_start()
#==============================================================================
# The logic *we* need is already taken care of by the UploadHook's RegisterCleanup,
# so we can just leave this stub for subclassing:
lib/Apache2/ASP/UploadHandler.pm view on Meta::CPAN
#==============================================================================
sub upload_hook
{
my ($s, $context, $Upload) = @_;
return;
# Since this method may be called several times per second, we only
# want to save the Session state once per second:
my $Diff = time() - $LastUpdate;
my $PercentDiff = $Upload->{percent_complete} - $LastPercent;
if( $Diff >= 1 || $PercentDiff >= 5 )
{
my $uploadID = $s->_args('uploadID') || '';
# Store everything in the session except for the data
# (since that could be too large to serialize quickly):
$context->session->{"upload$uploadID$_"} = $Upload->{$_}
foreach grep { $_ !~ m/data/ } keys(%$Upload);
$context->session->save;
$LastUpdate = time();
$LastPercent = $Upload->{percent_complete};
}# end if()
}# end upload_hook()
#==============================================================================
sub _args
{
my ($s, $key) = @_;
my %args = map {
lib/Apache2/ASP/UploadHook.pm view on Meta::CPAN
#==============================================================================
sub hook
{
my ($s, $upload, $data) = @_;
my $length_received = defined($data) ? length($data) : 0;
my $context = $s->context;
my $CONTENT_LENGTH = $ENV{CONTENT_LENGTH} || $context->r->pnotes('content_length');
my $total_loaded = ($context->r->pnotes('total_loaded') || 0) + $length_received;
$context->r->pnotes( total_loaded => $total_loaded);
my $percent_complete = sprintf("%.2f", $total_loaded / $CONTENT_LENGTH * 100 );
# Mark our start time, so we can make our calculations:
my $start_time = $context->r->pnotes('upload_start_time');
if( ! $start_time )
{
$start_time = gettimeofday();
$context->r->pnotes('upload_start_time' => $start_time);
}# end if()
# Calculate elapsed, total expected and remaining time, etc:
my $elapsed_time = gettimeofday() - $start_time;
my $bytes_per_second = $context->r->pnotes('total_loaded') / $elapsed_time;
$bytes_per_second ||= 1;
my $total_expected_time = int( ($CONTENT_LENGTH - $length_received) / $bytes_per_second );
my $time_remaining = int( (100 - $percent_complete) * $total_expected_time / 100 );
$time_remaining = 0 if $time_remaining < 0;
# Use an object, not just a hashref:
my $Upload = Apache2::ASP::UploadHookArgs->new(
upload => $upload,
percent_complete => $percent_complete,
elapsed_time => $elapsed_time,
total_expected_time => $total_expected_time,
time_remaining => $time_remaining,
length_received => $length_received,
data => defined($data) ? $data : undef,
);
# Init the upload:
my $did_init = $ENV{did_init};
if( ! $did_init )
lib/Apache2/ASP/UploadHookArgs.pm view on Meta::CPAN
use strict;
use warnings 'all';
#==============================================================================
sub new
{
my ($s, %args) = @_;
exists($args{$_}) or die "Required parameter '$_' was not provided" foreach qw(
upload
percent_complete
elapsed_time
total_expected_time
time_remaining
length_received
data
);
$args{content_length} = $ENV{CONTENT_LENGTH};
$args{new_file} = undef;
$args{filename_only} = undef;
$args{link_to_file} = undef;
lib/Apache2/ASP/UploadHookArgs.pm view on Meta::CPAN
=pod
=head1 NAME
Apache2::ASP::UploadHookArgs - Argument for UploadHook instances
=head1 SYNOPSIS
my $Upload = Apache2::ASP::UploadHookArgs->new(
upload => $upload, # An APR::Request::Param::Table object
percent_complete => $percent_complete,
elapsed_time => $elapsed_time, # in seconds
total_expected_time => $total_expected_time, # in seconds
time_remaining => $time_remaining, # in seconds
length_received => $length_received, # in bytes
data => defined($data) ? $data : undef, # bytes received in this "chunk"
);
=head1 DESCRIPTION
Rather than just passing a hashref as an argument, this class serves to enforce some structure
lib/Apache2/ASP/UploadHookArgs.pm view on Meta::CPAN
=head1 METHODS
=head2 new( %args )
C<%args> should be as shown in the synopsis above.
=head2 upload( )
Returns an L<APR::Request::Param::Table> object.
=head2 percent_complete( )
Returns a float representing what percent of the upload has been received so far.
=head2 elapsed_time( )
Returns the number of seconds since the upload began.
=head2 total_expected_time( )
Returns the total number of seconds we expect the upload to last.
=head2 time_remaining( )
( run in 0.449 second using v1.01-cache-2.11-cpan-10c994e2082 )