Apache-AxKit-Plugin-Upload
view release on metacpan or search on metacpan
lib/Apache/AxKit/Plugin/Upload.pm view on Meta::CPAN
package Apache::AxKit::Plugin::Upload;
use strict;
use Apache::Constants qw(:common);
use Number::Format;
use Fcntl qw(LOCK_EX LOCK_NB O_RDWR O_CREAT);
use IO::Handle;
BEGIN {
our $VERSION = 0.2;
}
eval {
require Time::HiRes;
};
if (!$@) {
use Time::HiRes;
}
my ($start, $sizetotal, $file, $location, $nf, $custom, $running, $lock);
sub print_html {
my ($now, $sizedone) = @_;
my $elapsed = $now-$start;
my $todo = 0;
$todo = ($sizetotal - $sizedone) * $elapsed / $sizedone if $sizedone > 0;
my $percent = 0;
$percent = int($sizedone*100/$sizetotal) if $sizetotal > 0;
my $showpercent = ($percent?$percent:1);
#warn("printing: $percent");
my $speed = 0;
$speed = $nf->format_bytes($sizedone / $elapsed,1) if $elapsed > 0;
$sizedone = $nf->format_bytes($sizedone,1);
my $sizetotal = $nf->format_bytes($sizetotal,1);
$elapsed = sprintf("%i:%02i",int($elapsed/60),$elapsed%60);
$todo = sprintf("%i:%02i",int($todo/60),$todo%60);
local(*FH);
open(FH, ">$file.tmp") || warn("could not open $file.tmp: $!");
if (!$custom && $percent == 100) {
print FH << "EOF";
<html>
<head>
<title>Upload Status</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<table border="0" width="264" cellspacing="0" cellpadding="1" style="table-layout: fixed;">
<tr>
<td width="40" height="24"><font face="sans-serif">$percent\%</font></td>
<td width="224" height="24" bgcolor="#000000"><table border="0" width="222" cellspacing="0" cellpadding="1" style="table-layout: fixed;" bgcolor="#ffffff">
<tr>
<td height="22"><table border="0" width="$showpercent\%" cellspacing="0" cellpadding="0" style="table-layout: fixed;">
<tr><td height="20" bgcolor="#000080"><font size="-7"> </font></td></tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="center"><font face="sans-serif" size="-1">$sizedone / $sizetotal, ${speed}b/s, $todo</font></td>
</tr>
</table>
<font color="#ffffff"><script language="javascript">window.close()</script></font>
</body>
</html>
EOF
} else {
print FH << "EOF";
<html>
<head>
<title>Upload Status</title>
<meta http-equiv="Refresh" CONTENT="1; URL=$location" />
</head>
<body bgcolor="#ffffff" text="#000000">
<table border="0" width="264" cellspacing="0" cellpadding="1" style="table-layout: fixed;">
<tr>
<td width="40" height="24"><font face="sans-serif">$percent\%</font></td>
<td width="224" height="24" bgcolor="#000000"><table border="0" width="222" cellspacing="0" cellpadding="1" style="table-layout: fixed;" bgcolor="#ffffff">
<tr>
<td height="22"><table border="0" width="$showpercent\%" cellspacing="0" cellpadding="0" style="table-layout: fixed;">
<tr><td height="20" bgcolor="#000080"><font size="-7"> </font></td></tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="center"><font face="sans-serif" size="-1">$sizedone / $sizetotal, ${speed}b/s, $todo</font></td>
</tr>
</table>
</body>
</html>
EOF
}
rename("$file.tmp",$file);
}
sub init {
my ($r,$upload_id) = @_;
my $destdir = $r->dir_config('AxUploadStatusDir') || return 0;
$destdir = $r->document_root.'/'.$destdir if substr($destdir,0,1) ne '/';
my $destloc = $r->dir_config('AxUploadStatusLocation') || return 0;
my $format = lc($r->dir_config('AxUploadFormat')) || 'html';
$location = "$destloc/$upload_id.$format";
$file = "$destdir/$upload_id.$format";
local(*FH);
open(FH, ">$file.tmp") || warn("could not open $file.tmp: $!");
print FH << "EOF";
<html>
<head>
<title>Upload Status</title>
<meta http-equiv="Refresh" CONTENT="1; URL=$location" />
</head>
<body bgcolor="#ffffff" text="#ffffff">
</body>
</html>
EOF
close(FH);
rename("$file.tmp",$file);
}
sub progress {
my ($done, $total, $message) = @_;
my $now = time();
my $elapsed = $now-$start;
my $todo = 0;
$todo = ($total - $done) * $elapsed / $done if $done > 0;
my $percent = 0;
$percent = int($done*100/$total) if $total > 0;
my $showpercent = ($percent?$percent:1);
$todo = sprintf("%i:%02i",int($todo/60),$todo%60);
local(*FH);
open(FH, ">$file.tmp") || warn("could not open $file.tmp: $!");
if ($percent == 100) {
print FH << "EOF";
<html>
<head>
<title>Upload Status</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<table border="0" width="264" cellspacing="0" cellpadding="1" style="table-layout: fixed;">
<tr>
<td width="40" height="24"><font face="sans-serif" size="-1">$percent\%</font></td>
<td width="224" height="24" bgcolor="#000000"><table border="0" width="222" cellspacing="0" cellpadding="1" style="table-layout: fixed;" bgcolor="#ffffff">
<tr>
<td height="22"><table border="0" width="$showpercent\%" cellspacing="0" cellpadding="0" style="table-layout: fixed;">
<tr><td height="20" bgcolor="#000080"><font size="-7"> </font></td></tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="center"><font face="sans-serif" size="-1">$message</font></td>
</tr>
</table>
<font color="#ffffff"><script language="javascript">window.close()</script></font>
</body>
</html>
EOF
close($lock);
} else {
print FH << "EOF";
<html>
<head>
<title>Upload Status</title>
<meta http-equiv="Refresh" CONTENT="1; URL=$location" />
</head>
<body bgcolor="#ffffff" text="#000000">
<table border="0" width="264" cellspacing="0" cellpadding="1" style="table-layout: fixed;">
<tr>
<td width="40" height="24"><font face="sans-serif" size="-1">$percent\%</font></td>
<td width="224" height="24" bgcolor="#000000"><table border="0" width="222" cellspacing="0" cellpadding="1" style="table-layout: fixed;" bgcolor="#ffffff">
<tr>
<td height="22"><table border="0" width="$showpercent\%" cellspacing="0" cellpadding="0" style="table-layout: fixed;">
<tr><td height="20" bgcolor="#000080"><font size="-7"> </font></td></tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="center"><font face="sans-serif" size="-1">$message</font></td>
</tr>
</table>
</body>
</html>
EOF
}
close(FH);
rename("$file.tmp",$file);
}
sub is_running {
my ($r,$upload_id) = @_;
return 0 if ($running eq $upload_id);
my $destdir = $r->dir_config('AxUploadStatusDir') || return 0;
$destdir = $r->document_root.'/'.$destdir if substr($destdir,0,1) ne '/';
my $format = lc($r->dir_config('AxUploadFormat')) || 'html';
$file = $destdir."/".$upload_id.".".$format;
local (*FH);
sysopen(FH,$file.".lck",O_RDWR) || return undef;
my $unlocked = flock(FH,LOCK_EX|LOCK_NB);
close(FH);
return !$unlocked;
}
sub upload_handler_html {
my ($upload, $buf, $len, $hook_data) = @_;
my ($sizedone, $lasttime) = @$hook_data;
my $now = time();
$sizedone += $len;
$$hook_data[0] = $sizedone;
#warn("now: $now, done: $sizedone, last: $lasttime");
return if (int($now) == $lasttime);
$$hook_data[1] = int($now);
print_html($now, $sizedone);
}
sub handler {
my ($r) = @_;
my %args = $r->args;
my $upload_id = $args{'axkit_upload_id'};
$running = '';
my $destdir = $r->dir_config('AxUploadStatusDir') || return OK;
$destdir = $r->document_root.'/'.$destdir if substr($destdir,0,1) ne '/';
my $destloc = $r->dir_config('AxUploadStatusLocation') || return OK;
my $format = lc($r->dir_config('AxUploadFormat')) || 'html';
$custom = $r->dir_config('AxUploadCustom');
$file = "$destdir/$upload_id.$format";
( run in 2.017 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )