Skip to content

Commit

Permalink
u3d: refactor out download_file functionality into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Aug 31, 2017
1 parent fcabfea commit 97ea10b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
36 changes: 1 addition & 35 deletions lib/u3d/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
# SOFTWARE.
## --- END LICENSE BLOCK ---

require 'net/http'
require 'u3d/utils'
require 'u3d/download_validator'

module U3d
# Take care of downloading files and packages
# rubocop:disable ModuleLength
module Downloader
# Name of the directory for the package downloading
DOWNLOAD_DIRECTORY = 'Unity_Packages'.freeze
Expand Down Expand Up @@ -126,38 +124,7 @@ def get_package(downloader, validator, package, definition, files)
end

def download_package(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
Utils.download_file(path, url, size: size)
rescue Interrupt => e
# Ensure that the file is deleted if download is aborted
File.delete path
Expand Down Expand Up @@ -207,5 +174,4 @@ def url_for(package, definition)
end
end
end
# rubocop:enable ModuleLength
end
37 changes: 37 additions & 0 deletions lib/u3d/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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
Expand Down Expand Up @@ -58,6 +59,41 @@ def get_ssl(url, redirect_limit: 10)
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)
uri = URI(url)
size = nil
Expand Down Expand Up @@ -128,4 +164,5 @@ def pretty_filesize(filesize)
end
end
end
# rubocop:enable ModuleLength
end

0 comments on commit 97ea10b

Please sign in to comment.