better download progress with estimated time
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 31 Jul 2009 23:22:05 +0000 (23:22 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 31 Jul 2009 23:22:05 +0000 (23:22 +0000)
lib/PXElator/httpd.pm

index 3936813..781ef8c 100644 (file)
@@ -48,8 +48,6 @@ sub static {
 
        return if ! -f $full;
 
-       my $start_t = time();
-
        if ( my $pid = fork ) {
                # parent
                close($client);
@@ -65,8 +63,6 @@ sub static {
 
        my $size = -s $full || return;
 
-       $client->autoflush(1);
-
        print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
 
        open(my $fh, $full);
@@ -75,21 +71,31 @@ sub static {
        my $buff;
        my $pos = 0;
 
-       print "static $path $type $size block: $block\n";
+       STDERR->autoflush(1);
+       warn "static $path $type $size block: $block\n";
+
+       my $start_t = time();
+       my $last_t = $start_t;
 
        while( my $len = read $fh, $buff, $block ) {
-               print $client $buff;
+               syswrite $client,$buff;
+               $client->flush;
                $pos += $len;
-               printf "%s %d/%d %.2f%% %.2f K/s\r"
+               my $t = time();
+               next unless $t - $last_t > 0.75;
+               $last_t = $t;
+               my $speed = ( $pos ) / ( $t - $start_t );
+               printf STDERR "%s %d/%d %.2f%% %.2f K/s ETA %.1fs\r"
                        , $path, $pos
                        , $size, $pos * 100 / $size
-                       , ( $pos / 1024 ) / ( time() - $start_t )
+                       , $speed / 1024
+                       , ( $size - $pos ) / $speed
                        ;
        }
        close($fh);
        close($client);
 
-       print "\n";
+       print STDERR "\n";
 
        exit;
 }