lib/u3d/utils.rb in u3d-1.0.1 vs lib/u3d/utils.rb in u3d-1.0.2

- old
+ new

@@ -25,10 +25,11 @@ require 'filesize' require 'u3d_core/helper' module U3d # Several different utility methods + # rubocop:disable ModuleLength module Utils # Regex to capture each part of a version string (0.0.0x0) CSIDL_LOCAL_APPDATA = 0x001c UNITY_VERSION_REGEX = /(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:(\w)(?:(\d+))?)?/ @@ -56,17 +57,54 @@ get_ssl(response['location'], redirect_limit: redirect_limit - 1) else raise "Request failed with status #{response.code}" end end + def download_file(path, url, size: nil) + File.open(path, 'wb') do |f| + uri = URI(url) + current = 0 + last_print_update = 0 + 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']) + 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| + f.write(segment) + 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 UI.interactive? + next unless Time.now.to_f - last_print_update > 0.5 + 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 + end + end + end + print "\n" if UI.interactive? + end + end + def get_url_content_length(url) + UI.verbose "get_url_content_length #{url}" uri = URI(url) size = nil Net::HTTP.start(uri.host, uri.port) do |http| response = http.request_head url size = Integer(response['Content-Length']) end + UI.verbose "get_url_content_length #{url}: #{size}" size end def hashfile(file_path, blocksize: 65_536) require 'digest' @@ -126,6 +164,7 @@ def pretty_filesize(filesize) Filesize.from(filesize.round.to_s + ' B').pretty end end end + # rubocop:enable ModuleLength end