All pastes #919102 Raw Edit

Unnamed

public text v1 · immutable
#919102 ·published 2008-02-26 16:27 UTC
rendered paste body
#!/usr/bin/perl 
#use strict;
#use POSIX 'SEEK_SET';
#use vars qw/ $uploadsFolder /;

use Fcntl qw(:DEFAULT :flock);
use File::Temp qw/ tempfile tempdir /;
use CGI;

my $path = $ENV{'PATH_TRANSLATED'} || '';
if($path eq ''){$path = $ENV{'SCRIPT_FILENAME'};}
$path =~s/upload.pl/conf.uploader/;
if($path eq ''){$path = "conf.uploader";}

my $DEBUG = 0;

print "Content-Type: text/plain\n\n";
warn "we required $path" if $DEBUG;
require $path;

my $content_type = $ENV{'CONTENT_TYPE'};
my $len = $ENV{'CONTENT_LENGTH'};
my $bRead = 0;
my $sessionid;

my $starttime = time;
my @qstring=split(/&/,$ENV{'QUERY_STRING'});
$sessionid = $qstring[0];

if($sessionid eq 'test'){
	print "ok";
	exit;
}

my $tmpfile   = "$uploadsFolder\\temp_$sessionid";
my $statsfile = "$uploadsFolder\\stats_$sessionid.txt";
my $tmpfilepre= "$uploadsFolder\\$sessionid\_";

$tmpfile =~s/\\/\//g;
$statsfile =~s/\\/\//g;
$tmpfilepre =~s/\\/\//g;

open(TMP,"+>","$tmpfile") or die "can't open temp file $tmpfile: $!";
open(STATS,">","$statsfile") or die "can't open stats file $statsfile: $!";

my $rLen = 4096;
my $LINE;

# values for dl progression-small
my ($now,$elapsed,$percent,$estimate,$estTime,$percentEst);

if($len - $bRead < $rLen){
	$rLen = $len-$bRead;
}

while ($bRead < $len && read (STDIN ,$LINE, $rLen)) {
	$bRead += length $LINE;
	if($len - $bRead < $rLen){
		$rLen = $len-$bRead;
	}
	
	print TMP $LINE;
	
	$now = time;
	$elapsed = $now-$starttime;
	$percent = ($bRead/$len) * 100;

	if($elapsed >= 1){
		$estimate = $bRead / $elapsed; # in kb
	}else{
		$estimate = $bRead / .5;
	}

	$estTime = ($len-$bRead)/$estimate; #in sec
	$percentEst = $estimate/$len * 100; #in percent

	# % , % per second, kb estimate, est time remaining
	if($percent >= 99){
		$percent = 99;
	}

	seek(STATS,0,SEEK_SET);
	print STATS "$percent\n$percentEst\n$estimate\n$estTime\n$len\n\n";
}


if($bRead != $len){
	close(TMP);
	unlink $tmpfile or warn "Failed to unlink $tmpfile: $!";
	unlink $statsfile or warn "Failed to unlink $statsfile: $!";
	exit;
}

my ($junk,$boundary);
($junk,$boundary) = split /=/, $ENV{CONTENT_TYPE}, 2;
$boundary =~ s/\n//;
$boundary = "--$boundary";


seek(TMP,0,SEEK_SET);

my $i = 0;
my $expect_junk = 0;
my @fileCount;
my ($filename,$line,$headers,$unwanted);


LINE: while (<TMP>) {
	$line = $_;
	# If it's a boundary
	if (($line eq "$boundary\r\n") && ($fileCount[$i] eq '')){
	close FILE;
	$i++;
	# register it's a new file
	print "got new filename" if $DEBUG;
	$fileCount[$i] = 'new';
	next LINE;
	# find the new filename
	} elsif ($fileCount[$i] eq 'new'){
		print "<br> suspect new filename on file ".$i if $DEBUG;
		($headers,$unwanted) = split(/\n\r\n/,$line,2);
		($headers,$unwanted) = split(/\n\r\n/,$line,2);
		$filename = $headers;
		$filename =~s/.*?\n?.+?filename=\"(.+(\\|\/))?(.+?)\".*\n.*/file-$3/g;
		if(substr($filename,0,5) eq "file-"){
			$filename =~s/file-//;
			print "<br>filename was $filename" if $DEBUG;
			open(FILE,"+>","$tmpfilepre$filename") or  warn "Can't open temp file: $tmpfilepre$filename: $!";
			binmode(FILE);
			$fileCount[$i] = '';
		} else { 
			if ($DEBUG) {warn "something wrong\n"; print "<br>something is wrong"; }
		}
	} elsif ($expect_junk == 1) {
		$expect_junk = 0;
		next LINE;
	} else {
		if ($line =~ m/Content-Type/) {
		$expect_junk = 1;
		next LINE;
		}
	# Real file data here, write it.
	print FILE $line or die "Failed to write file: $!";
	}
}

close FILE;
close TMP;
seek(STATS,0,SEEK_SET);
print STATS "100\n$percentEst\n$estimate\n$estTime\n$len\n";
close STATS;