Handle case where upload never started
[webmin.git] / uptracker.cgi
1 #!/usr/local/bin/perl
2 # Output Javascript in a loop to track an upload
3 # XXX add to more modules
4
5 require './web-lib.pl';
6 &init_config();
7 do './ui-lib.pl';
8 &ReadParse();
9 $id = $in{'id'};
10 $id || &error($text{'uptracker_eid'});
11 $id !~ /\.\./ && $id !~ /\0/ || &error($text{'uptracker_eid2'});
12
13 &popup_header($text{'uptracker_title'}, undef,
14               "onunload='if (!window.doneupload) { opener.stop() }'");
15 $| = 1;
16
17 # Output text boxes that get updated with filenames and progress
18 $ff = "style='font-family: courier'";
19 print "<form>\n";
20 print "<center><table>\n";
21 print "<tr> <td><b>$text{'uptracker_file'}</b></td>\n";
22 print "<td>",&ui_textbox("file", undef, 50, 1, undef, $ff),"</td> </tr>\n";
23 print "<tr> <td><b>$text{'uptracker_size'}</b></td>\n";
24 print "<td>",&ui_textbox("size", undef, 50, 1, undef, $ff),"</td> </tr>\n";
25 print "<tr> <td><b>$text{'uptracker_pc'}</b></td>\n";
26 print "<td>",&ui_textbox("pc", undef, 50, 1, undef, $ff),"</td> </tr>\n";
27 print "</table></center>\n";
28 print "</form>\n";
29
30 # Find the location of the user's upload progess file
31 if ($in{'uid'}) {
32         @uinfo = getpwuid($in{'uid'});
33         $upfile = "$uinfo[7]/.tmp/upload.$id";
34         }
35 else {
36         $upfile = "$ENV{'WEBMIN_VAR'}/upload.$id";
37         }
38
39 # Read the tracker file in a loop until done, or until 1 minute has passed
40 # with no progress
41 print "<script>\n";
42 print "window.doneupload = 1;\n";
43 print "</script>\n";
44 $start = time();
45 while(1) {
46         sleep(1);
47         $now = time();
48         if (!open(UPFILE, $upfile)) {
49                 # Doesn't exist yet
50                 if ($now - $start > 60) {
51                         # Give up after 60 seconds
52                         print "<script>\n";
53                         print "document.forms[0].pc.value = \"Not started\";\n";
54                         print "</script>\n";
55                         last;
56                         }
57                 next;
58                 }
59         @lines = <UPFILE>;
60         chop(@lines);
61         close(UPFILE);
62         ($size, $totalsize, $filename) = @lines;
63         if ($size == -1) {
64                 # Come to the end OK .. set percent bar to 100
65                 print "<script>\n";
66                 print "document.forms[0].pc.value = \"".("X" x 50)."\";\n";
67                 print "window.doneupload = 1;\n";
68                 print "</script>\n";
69                 last;
70                 }
71
72         # Check if there has been no activity for 60 seconds
73         if ($size == $last_size) {
74                 if ($last_time && $last_time < $now-60) {
75                         # Too slow! Give up
76                         print "<script>\n";
77                         print "document.forms[0].pc.value = \"Timeout\";\n";
78                         print "</script>\n";
79                         last;
80                         }
81                 }
82         else {
83                 $last_size = $size;
84                 $last_time = $now;
85                 }
86
87         $pc = int(100 * $size / $totalsize) / 2;
88         next if (defined($lastpc) && $pc == $lastpc);
89         print "<script>\n";
90         print "document.forms[0].file.value = \"".
91                 &quote_escape($filename)."\";\n";
92         print "document.forms[0].size.value = \"".
93                 &quote_escape(&text('uptracker_of',
94                                     &nice_size($size),
95                                     &nice_size($totalsize)))."\";\n";
96         print "document.forms[0].pc.value = \"".("|" x $pc)."\";\n";
97         print "</script>\n";
98         
99         $lastpc = $pc;
100         last if ($size >= $totalsize);
101         }
102
103 # All done, so close the window and remove the file
104 print "<script>\n";
105 print "window.close();\n";
106 print "</script>\n";
107 unlink($upfile);
108
109 &popup_footer();
110