<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# download.pl
# Start downloading some file, and update the .down file with its progress

$no_acl_check++;
require './updown-lib.pl';

$down = &amp;get_download($ARGV[0]);
$down || die "Download ID $ARGV[0] does not exist!";
&amp;can_write_file($down-&gt;{'dir'}) || die "Cannot download files to $down-&gt;{'dir'}";

# Do the download, updating the config file with progress
$down-&gt;{'pid'} = $$;
&amp;save_download($down);
$error = &amp;do_download($down, \&amp;download_callback, \@paths);
$down-&gt;{'complete'} = 1;
$down-&gt;{'error'} = $error if ($error);
&amp;save_download($down);

sub download_callback
{
if ($_[0] == 1) {
	# Started ok
	delete($down-&gt;{'size'});
	delete($down-&gt;{'got'});
	delete($down-&gt;{'finished'});
	$lastupdate = 0;
	$lastpercent = 0;
	}
elsif ($_[0] == 2) {
	# Got size
	$down-&gt;{'size'} = $_[1];
	}
elsif ($_[0] == 3) {
	# Got some data. Only update the status file every 10 seconds or when
	# a percent of data is received
	$down-&gt;{'got'} = $_[1];
	if ($down-&gt;{'size'}) {
		$percent = int($down-&gt;{'got'}*100/$down-&gt;{'size'});
		$now = time();
		return if ($percent &lt;= $lastpercent &amp;&amp;
			   $now &lt; $lastupdate + 10);
		$lastupdate = $now;
		$lastpercent = $percent;
		}
	}
elsif ($_[0] == 4) {
	# All done
	$down-&gt;{'finished'} = 1;
	$down-&gt;{'total'} += $down-&gt;{'got'};
	}
elsif ($_[0] == 5) {
	# Redirecting to new URL
	}
&amp;switch_uid_back();
&amp;save_download($down);
&amp;switch_uid_to($down-&gt;{'uid'}, $down-&gt;{'gid'});
}

</pre></body></html>