|
25 | 25 | my $ok;
|
26 | 26 |
|
27 | 27 | my $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq "y";
|
| 28 | +my $custom_tool = $ENV{DOWNLOAD_TOOL_CUSTOM}; |
| 29 | +my $download_tool; |
28 | 30 |
|
29 | 31 | $url_filename or $url_filename = $filename;
|
30 | 32 |
|
@@ -85,35 +87,54 @@ sub tool_present {
|
85 | 87 | return $present
|
86 | 88 | }
|
87 | 89 |
|
| 90 | +sub select_tool { |
| 91 | + $custom_tool =~ tr/"//d; |
| 92 | + if ($custom_tool) { |
| 93 | + return $custom_tool; |
| 94 | + } |
| 95 | + |
| 96 | + # Try to use curl if available |
| 97 | + if (tool_present("curl", "curl")) { |
| 98 | + return "curl"; |
| 99 | + } |
| 100 | + |
| 101 | + # No tool found, fallback to wget |
| 102 | + return "wget"; |
| 103 | +} |
| 104 | + |
88 | 105 | sub download_cmd {
|
89 | 106 | my $url = shift;
|
90 | 107 | my $filename = shift;
|
91 |
| - my $additional_mirrors = join(" ", map "$_/$filename", @_); |
92 | 108 |
|
93 |
| - my @chArray = ('a'..'z', 'A'..'Z', 0..9); |
94 |
| - my $rfn = join '', "${filename}_", map{ $chArray[int rand @chArray] } 0..9; |
| 109 | + if ($download_tool eq "curl") { |
| 110 | + return (qw(curl -f --connect-timeout 20 --retry 5 --location), |
| 111 | + $check_certificate ? () : '--insecure', |
| 112 | + shellwords($ENV{CURL_OPTIONS} || ''), |
| 113 | + $url); |
| 114 | + } elsif ($download_tool eq "wget") { |
| 115 | + return (qw(wget --tries=5 --timeout=20 --output-document=-), |
| 116 | + $check_certificate ? () : '--no-check-certificate', |
| 117 | + shellwords($ENV{WGET_OPTIONS} || ''), |
| 118 | + $url); |
| 119 | + } elsif ($download_tool eq "aria2c") { |
| 120 | + my $additional_mirrors = join(" ", map "$_/$filename", @_); |
| 121 | + my @chArray = ('a'..'z', 'A'..'Z', 0..9); |
| 122 | + my $rfn = join '', "${filename}_", map{ $chArray[int rand @chArray] } 0..9; |
95 | 123 |
|
96 |
| - if (tool_present('aria2c', 'aria2')) { |
97 | 124 | @mirrors=();
|
| 125 | + |
98 | 126 | return join(" ", "[ -d $ENV{'TMPDIR'}/aria2c ] || mkdir $ENV{'TMPDIR'}/aria2c;",
|
99 | 127 | "touch $ENV{'TMPDIR'}/aria2c/${rfn}_spp;",
|
100 | 128 | qw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $url, $additional_mirrors,
|
101 | 129 | $check_certificate ? () : '--check-certificate=false',
|
102 | 130 | "--server-stat-of=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
|
103 | 131 | "--server-stat-if=$ENV{'TMPDIR'}/aria2c/${rfn}_spp",
|
| 132 | + "--daemon=false --no-conf", shellwords($ENV{ARIA2C_OPTIONS} || ''), |
104 | 133 | "-d $ENV{'TMPDIR'}/aria2c -o $rfn;",
|
105 | 134 | "cat $ENV{'TMPDIR'}/aria2c/$rfn;",
|
106 | 135 | "rm $ENV{'TMPDIR'}/aria2c/$rfn $ENV{'TMPDIR'}/aria2c/${rfn}_spp");
|
107 |
| - } elsif (tool_present('curl', 'curl')) { |
108 |
| - return (qw(curl -f --connect-timeout 20 --retry 5 --location), |
109 |
| - $check_certificate ? () : '--insecure', |
110 |
| - shellwords($ENV{CURL_OPTIONS} || ''), |
111 |
| - $url); |
112 | 136 | } else {
|
113 |
| - return (qw(wget --tries=5 --timeout=20 --output-document=-), |
114 |
| - $check_certificate ? () : '--no-check-certificate', |
115 |
| - shellwords($ENV{WGET_OPTIONS} || ''), |
116 |
| - $url); |
| 137 | + return join(" ", $download_tool, $url); |
117 | 138 | }
|
118 | 139 | }
|
119 | 140 |
|
@@ -328,6 +349,8 @@ sub cleanup
|
328 | 349 | };
|
329 | 350 | }
|
330 | 351 |
|
| 352 | +$download_tool = select_tool(); |
| 353 | + |
331 | 354 | while (!-f "$target/$filename") {
|
332 | 355 | my $mirror = shift @mirrors;
|
333 | 356 | $mirror or die "No more mirrors to try - giving up.\n";
|
|
0 commit comments