Handle hostnames with upper-case letters
[webmin.git] / updown / download.pl
1 #!/usr/local/bin/perl
2 # download.pl
3 # Start downloading some file, and update the .down file with its progress
4
5 $no_acl_check++;
6 require './updown-lib.pl';
7
8 $down = &get_download($ARGV[0]);
9 $down || die "Download ID $ARGV[0] does not exist!";
10 &can_write_file($down->{'dir'}) || die "Cannot download files to $down->{'dir'}";
11
12 # Do the download, updating the config file with progress
13 $down->{'pid'} = $$;
14 &save_download($down);
15 $error = &do_download($down, \&download_callback, \@paths);
16 $down->{'complete'} = 1;
17 $down->{'error'} = $error if ($error);
18 &save_download($down);
19
20 sub download_callback
21 {
22 if ($_[0] == 1) {
23         # Started ok
24         delete($down->{'size'});
25         delete($down->{'got'});
26         delete($down->{'finished'});
27         $lastupdate = 0;
28         $lastpercent = 0;
29         }
30 elsif ($_[0] == 2) {
31         # Got size
32         $down->{'size'} = $_[1];
33         }
34 elsif ($_[0] == 3) {
35         # Got some data. Only update the status file every 10 seconds or when
36         # a percent of data is received
37         $down->{'got'} = $_[1];
38         if ($down->{'size'}) {
39                 $percent = int($down->{'got'}*100/$down->{'size'});
40                 $now = time();
41                 return if ($percent <= $lastpercent &&
42                            $now < $lastupdate + 10);
43                 $lastupdate = $now;
44                 $lastpercent = $percent;
45                 }
46         }
47 elsif ($_[0] == 4) {
48         # All done
49         $down->{'finished'} = 1;
50         $down->{'total'} += $down->{'got'};
51         }
52 elsif ($_[0] == 5) {
53         # Redirecting to new URL
54         }
55 &switch_uid_back();
56 &save_download($down);
57 &switch_uid_to($down->{'uid'}, $down->{'gid'});
58 }
59