downloader.rb: add file:// protocol support, fallback to curl when the protocol is not http(s):///file:// (#6520)

* Update downloader.rb

* Update const.rb
This commit is contained in:
supechicken
2021-12-30 16:01:21 +08:00
committed by GitHub
parent 2294f3d539
commit 0b29bab4a2
2 changed files with 23 additions and 1 deletions

View File

@@ -21,6 +21,28 @@ def downloader (url, filename = File.basename(url), retry_count = 0, verbose = f
trap('WINCH') { setTermSize }
uri = URI(url)
case uri.scheme
when /http?/
when 'file'
# use FileUtils to copy if it is a local file (the url protocol is file://)
if File.exist?(uri.path)
return FileUtils.cp uri.path, filename
else
abort "#{uri.path}: File not found :/".lightred
end
else
# fallback to curl if the url protocol is not http(s):// or file://
0.step(3,1) do |i|
unless system "#{CURL} --ssl -#L -C - '#{uri.to_s}' -o '#{filename}'"
puts "Retrying, #{3-i} retries left.".yellow
else
return true
end
end
# the download failed if we're still here
abort 'Download failed :/ Please check your network settings.'.lightred
end
Net::HTTP.start(uri.host, uri.port, {
max_retries: 3,
use_ssl: uri.scheme.eql?('https'),