lib/u3d/utils.rb in u3d-1.0.9 vs lib/u3d/utils.rb in u3d-1.0.10
- old
+ new
@@ -57,21 +57,24 @@
get_ssl(response['location'], redirect_limit: redirect_limit - 1)
else raise "Request failed with status #{response.code}"
end
end
+ # size a hint of the expected size
def download_file(path, url, size: nil)
File.open(path, 'wb') do |f|
uri = URI(url)
current = 0
last_print_update = 0
print_progress = UI.interactive? || U3dCore::Globals.verbose?
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
begin
- size ||= Integer(response['Content-Length'])
+ # override with actual results, this should help with
+ # innacurrate declared sizes, especially on Windows platform
+ size = Integer(response['Content-Length'])
rescue ArgumentError
UI.verbose 'Unable to get length of file in download'
end
started_at = Time.now.to_i - 1
response.read_body do |segment|
@@ -79,17 +82,16 @@
current += segment.length
# wait for Net::HTTP buffer on slow networks
# FIXME revisits, this slows down download on fast network
# sleep 0.08 # adjust to reduce CPU
next unless print_progress
- next unless Time.now.to_f - last_print_update > 0.5
+ print_progress_now = Time.now.to_f - last_print_update > 0.5
+ # force printing when done downloading
+ print_progress_now = true if !print_progress_now && size && current >= size
+ next unless print_progress_now
last_print_update = Time.now.to_f
- if size
- Utils.print_progress(current, size, started_at)
- else
- Utils.print_progress_nosize(current, started_at)
- end
+ Utils.print_progress(current, size, started_at)
print "\n" unless UI.interactive?
end
end
end
print "\n" if print_progress
@@ -120,10 +122,15 @@
def ensure_dir(dir)
FileUtils.mkpath(dir) unless File.directory?(dir)
end
+ # if total is nil (unknown, falls back to print_progress_nosize)
def print_progress(current, total, started_at)
+ if total.nil?
+ print_progress_nosize(current, started_at)
+ return
+ end
ratio = [current.to_f / total, 1.0].min
percent = (ratio * 100.0).round(1)
arrow = (ratio * 20.0).floor
time_spent = Time.now.to_i - started_at
print("\r[")