lib/u3d/downloader.rb in u3d-1.0.1 vs lib/u3d/downloader.rb in u3d-1.0.2
- old
+ new
@@ -18,17 +18,15 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# 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
# Path to the directory for the package downloading
DOWNLOAD_PATH = "#{ENV['HOME']}/Downloads".freeze
@@ -113,53 +111,22 @@
end
end
UI.header "Downloading #{package} version #{definition.version}"
UI.message 'Downloading from ' + url.to_s.cyan.underline
- download_package(path, url, size: definition.size_in_kb(package))
+ download_package(path, url, size: definition.size_in_bytes(package))
if validator.validate(package, path, definition)
UI.success "Successfully downloaded #{package}."
files << [package, path, definition[package]]
else
UI.error "Failed to download #{package}"
end
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
raise e
end
@@ -205,7 +172,6 @@
def url_for(package, definition)
definition.url + definition[package]['url']
end
end
end
- # rubocop:enable ModuleLength
end