Skip to content

Commit baa20ae

Browse files
fix(install): Fix aria2's resume download feature (#3292)
Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
1 parent 77d00d1 commit baa20ae

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

lib/install.ps1

+42-38
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
205205
# aria2 input file
206206
$urlstxt = Join-Path $cachedir "$app.txt"
207207
$urlstxt_content = ''
208-
$has_downloads = $false
208+
$download_finished = $true
209209

210210
# aria2 options
211211
$options = @(
@@ -225,64 +225,66 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
225225
"--min-tls-version=TLSv1.2"
226226
"--stop-with-process=$PID"
227227
"--continue"
228-
"--summary-interval 0"
228+
"--summary-interval=0"
229+
"--auto-save-interval=1"
229230
)
230231

231-
if($cookies) {
232+
if ($cookies) {
232233
$options += "--header='Cookie: $(cookie_header $cookies)'"
233234
}
234235

235236
$proxy = get_config 'proxy'
236-
if($proxy -ne 'none') {
237-
if([Net.Webrequest]::DefaultWebProxy.Address) {
237+
if ($proxy -ne 'none') {
238+
if ([Net.Webrequest]::DefaultWebProxy.Address) {
238239
$options += "--all-proxy='$([Net.Webrequest]::DefaultWebProxy.Address.Authority)'"
239240
}
240-
if([Net.Webrequest]::DefaultWebProxy.Credentials.UserName) {
241+
if ([Net.Webrequest]::DefaultWebProxy.Credentials.UserName) {
241242
$options += "--all-proxy-user='$([Net.Webrequest]::DefaultWebProxy.Credentials.UserName)'"
242243
}
243-
if([Net.Webrequest]::DefaultWebProxy.Credentials.Password) {
244+
if ([Net.Webrequest]::DefaultWebProxy.Credentials.Password) {
244245
$options += "--all-proxy-passwd='$([Net.Webrequest]::DefaultWebProxy.Credentials.Password)'"
245246
}
246247
}
247248

248249
$more_options = get_config 'aria2-options'
249-
if($more_options) {
250+
if ($more_options) {
250251
$options += $more_options
251252
}
252253

253-
foreach($url in $urls) {
254+
foreach ($url in $urls) {
254255
$data.$url = @{
255-
'filename' = url_filename $url
256-
'target' = "$dir\$(url_filename $url)"
256+
'target' = "$dir\$(url_filename $url)"
257257
'cachename' = fname (cache_path $app $version $url)
258-
'source' = fullpath (cache_path $app $version $url)
258+
'source' = fullpath (cache_path $app $version $url)
259259
}
260260

261-
if(!(test-path $data.$url.source)) {
262-
$has_downloads = $true
261+
if ((Test-Path $data.$url.source) -and -not((Test-Path "$($data.$url.source).aria2") -or (Test-Path $urlstxt)) -and $use_cache) {
262+
Write-Host 'Loading ' -NoNewline
263+
Write-Host $(url_remote_filename $url) -f Cyan -NoNewline
264+
Write-Host ' from cache.'
265+
} else {
266+
$download_finished = $false
263267
# create aria2 input file content
264268
$urlstxt_content += "$(handle_special_urls $url)`n"
265-
if(!$url.Contains('sourceforge.net')) {
269+
if (!$url.Contains('sourceforge.net')) {
266270
$urlstxt_content += " referer=$(strip_filename $url)`n"
267271
}
268272
$urlstxt_content += " dir=$cachedir`n"
269273
$urlstxt_content += " out=$($data.$url.cachename)`n"
270-
} else {
271-
Write-Host "Loading " -NoNewline
272-
Write-Host $(url_remote_filename $url) -f Cyan -NoNewline
273-
Write-Host " from cache."
274274
}
275275
}
276276

277-
if($has_downloads) {
277+
if (-not($download_finished)) {
278278
# write aria2 input file
279-
Set-Content -Path $urlstxt $urlstxt_content
279+
if ($urlstxt_content -ne '') {
280+
Set-Content -Path $urlstxt $urlstxt_content
281+
}
280282

281283
# build aria2 command
282284
$aria2 = "& '$(Get-HelperPath -Helper Aria2)' $($options -join ' ')"
283285

284286
# handle aria2 console output
285-
Write-Host "Starting download with aria2 ..."
287+
Write-Host 'Starting download with aria2 ...'
286288

287289
Invoke-Expression $aria2 | ForEach-Object {
288290
# Skip blank lines
@@ -311,50 +313,52 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
311313
error "Download failed! (Error $lastexitcode) $(aria_exit_code $lastexitcode)"
312314
error $urlstxt_content
313315
error $aria2
314-
abort $(new_issue_msg $app $bucket "download via aria2 failed")
316+
abort $(new_issue_msg $app $bucket 'download via aria2 failed')
315317
}
316318

317319
# remove aria2 input file when done
318-
if(test-path($urlstxt)) {
319-
Remove-Item $urlstxt
320+
if (Test-Path $urlstxt, "$($data.$url.source).aria2*") {
321+
Remove-Item $urlstxt -Force -ErrorAction SilentlyContinue
322+
Remove-Item "$($data.$url.source).aria2*" -Force -ErrorAction SilentlyContinue
320323
}
321324
}
322325

323-
foreach($url in $urls) {
326+
foreach ($url in $urls) {
324327

325328
$metalink_filename = get_filename_from_metalink $data.$url.source
326-
if($metalink_filename) {
329+
if ($metalink_filename) {
327330
Remove-Item $data.$url.source -Force
328331
Rename-Item -Force (Join-Path -Path $cachedir -ChildPath $metalink_filename) $data.$url.source
329332
}
330333

331334
# run hash checks
332-
if($check_hash) {
335+
if ($check_hash) {
333336
$manifest_hash = hash_for_url $manifest $url $architecture
334337
$ok, $err = check_hash $data.$url.source $manifest_hash $(show_app $app $bucket)
335-
if(!$ok) {
338+
if (!$ok) {
336339
error $err
337-
if(test-path $data.$url.source) {
340+
if (Test-Path $data.$url.source) {
338341
# rm cached file
339-
Remove-Item -force $data.$url.source
342+
Remove-Item $data.$url.source -Force -ErrorAction SilentlyContinue
343+
Remove-Item "$($data.$url.source).aria2*" -Force -ErrorAction SilentlyContinue
340344
}
341-
if($url.Contains('sourceforge.net')) {
345+
if ($url.Contains('sourceforge.net')) {
342346
Write-Host -f yellow 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.'
343347
}
344-
abort $(new_issue_msg $app $bucket "hash check failed")
348+
abort $(new_issue_msg $app $bucket 'hash check failed')
345349
}
346350
}
347351

348352
# copy or move file to target location
349-
if(!(test-path $data.$url.source) ) {
350-
abort $(new_issue_msg $app $bucket "cached file not found")
353+
if (!(Test-Path $data.$url.source) ) {
354+
abort $(new_issue_msg $app $bucket 'cached file not found')
351355
}
352356

353-
if(!($dir -eq $cachedir)) {
354-
if($use_cache) {
357+
if (!($dir -eq $cachedir)) {
358+
if ($use_cache) {
355359
Copy-Item $data.$url.source $data.$url.target
356360
} else {
357-
Move-Item $data.$url.source $data.$url.target -force
361+
Move-Item $data.$url.source $data.$url.target -Force
358362
}
359363
}
360364
}

0 commit comments

Comments
 (0)