Module: U3d::Downloader

Defined in:
lib/u3d/downloader.rb

Overview

Take care of downloading files and packages

Defined Under Namespace

Classes: LinuxDownloader, MacDownloader, WindowsDownloader

Constant Summary collapse

DOWNLOAD_DIRECTORY =

Name of the directory for the package downloading

'Unity_Packages'.freeze
DOWNLOAD_PATH =

Path to the directory for the package downloading

"#{ENV['HOME']}/Downloads".freeze
UNITY_MODULE_FILE_REGEX =

Regex to get the name of a package out of its file name

%r{\/([\w\-_\.\+]+\.(?:pkg|exe|zip|sh|deb))}

Class Method Summary collapse

Class Method Details

.download_package(path, url, size: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/u3d/downloader.rb', line 64

def download_package(path, url, size: nil)
  File.open(path, 'wb') do |f|
    uri = URI(url)
    current = 0
    Net::HTTP.start(uri.host, uri.port) 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
          next unless UI.interactive?
          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
rescue Interrupt => e
  # Ensure that the file is deleted if download is aborted
  File.delete path
  raise e
end

.hash_validation(expected: nil, actual: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/u3d/downloader.rb', line 38

def hash_validation(expected: nil, actual: nil)
  if expected
    if expected != actual
      UI.verbose "Expected hash is #{expected}, file hash is #{actual}"
      UI.important 'File looks corrupted (wrong hash)'
      return false
    end
  else
    UI.verbose 'No hash validation available. File is assumed correct but may not be.'
  end
  true
end

.size_validation(expected: nil, actual: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/u3d/downloader.rb', line 51

def size_validation(expected: nil, actual: nil)
  if expected
    if expected != actual
      UI.verbose "Expected size is #{expected}, file size is #{actual}"
      UI.important 'File looks corrupted (wrong size)'
      return false
    end
  else
    UI.verbose 'No size validation available. File is assumed correct but may not be.'
  end
  true
end