Simplify curl_download.

This commit is contained in:
Markus Reiter 2023-05-19 14:38:00 +02:00
parent 08cffe0a64
commit e7a9727f56
No known key found for this signature in database
GPG Key ID: 245293B51702655B

View File

@ -173,30 +173,30 @@ module Utils
destination = Pathname(to) destination = Pathname(to)
destination.dirname.mkpath destination.dirname.mkpath
args = ["--location", "--remote-time", "--output", destination, *args]
if try_partial if try_partial
headers = begin headers = begin
parsed_output = curl_headers(*args, **options, wanted_headers: ["accept-ranges"]) parsed_output = curl_headers(*args, **options, wanted_headers: ["accept-ranges"])
parsed_output.fetch(:responses).last&.fetch(:headers) parsed_output.fetch(:responses).last&.fetch(:headers) || {}
rescue ErrorDuringExecution rescue ErrorDuringExecution
nil # Ignore errors here and let actual download fail instead.
{}
end end
# Any value for `accept-ranges` other than none indicates that the server supports partial requests. # Any value for `Accept-Ranges` other than `none` indicates that the server
# Its absence indicates no support. # supports partial requests. Its absence indicates no support.
supports_partial = headers&.key?("accept-ranges") && headers["accept-ranges"] != "none" supports_partial = headers.fetch("accept-ranges", "none") != "none"
content_length = headers["content-length"]&.to_i
if supports_partial && if supports_partial && destination.exist?
destination.exist? && # We've already downloaded all bytes.
headers&.key?("content-length") && return if destination.size == content_length
destination.size == headers["content-length"].to_i
return # We've already downloaded all the bytes args = ["--continue-at", "-", *args]
end end
end end
args = ["--location", "--remote-time", "--output", destination, *args]
# continue-at shouldn't be used with servers that don't support partial requests.
args = ["--continue-at", "-", *args] if destination.exist? && supports_partial
curl(*args, **options) curl(*args, **options)
end end