diff options
| author | Eugen Wissner <belka@caraus.de> | 2023-01-04 10:51:08 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2023-01-04 10:51:08 +0100 |
| commit | ef9942fe994a219e75fe52e1a1d1098556a0d95a (patch) | |
| tree | 600832dc74ed08dbc20ccfc6a61802d67c06b4b6 /lib | |
| parent | 5b26e7dca87ca1eea4cc889e4ec1354398d75422 (diff) | |
| download | slackbuilder-ef9942fe994a219e75fe52e1a1d1098556a0d95a.tar.gz | |
Move download functions into a module
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/download.rb | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/lib/download.rb b/lib/download.rb index 308e8ee..64a453f 100644 --- a/lib/download.rb +++ b/lib/download.rb @@ -28,58 +28,58 @@ module SlackBuilder repository end -end -def write_chunk(response, checksum, progressbar, io) - response.read_body do |chunk| - progressbar.progress += chunk.length - io << chunk - checksum << chunk - end -end + def self.download(uri, target) + print Term::ANSIColor.green "Downloading #{uri} " + checksum = nil -def write_download(target, response) - checksum = Digest::MD5.new - progressbar = ProgressBar.create title: target, total: response.header.content_length + Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + checksum = start_download uri, target, http + end - File.open target, 'w' do |io| - write_chunk response, checksum, progressbar, io + puts + checksum end - progressbar.finish - checksum -end + private_class_method def self.redirect_download(location, target) + puts 'redirecting...' + new_location = URI location -def redirect_download(location, target) - puts 'redirecting...' - new_location = URI location + download new_location, target + end - download new_location, target -end + private_class_method def self.write_chunk(response, checksum, progressbar, io) + response.read_body do |chunk| + progressbar.progress += chunk.length + io << chunk + checksum << chunk + end + end -def start_download(uri, target, http) - request = Net::HTTP::Get.new uri + private_class_method def self.write_download(target, response) + checksum = Digest::MD5.new + progressbar = ProgressBar.create title: target, total: response.header.content_length - http.request request do |response| - case response - when Net::HTTPRedirection - return redirect_download response['location'], target - else - return write_download target, response + File.open target, 'w' do |io| + write_chunk response, checksum, progressbar, io end + progressbar.finish + + checksum end -end -def download(uri, target) - print Term::ANSIColor.green "Downloading #{uri} " - checksum = nil + private_class_method def self.start_download(uri, target, http) + request = Net::HTTP::Get.new uri - Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| - checksum = start_download uri, target, http + http.request request do |response| + case response + when Net::HTTPRedirection + return redirect_download response['location'], target + else + return write_download target, response + end + end end - - puts - checksum end def hosted_sources(absolute_url) @@ -135,10 +135,10 @@ def download_and_deploy(uri, tarball) if remote_file_exists?(remote_path) uri = URI hosted_sources(remote_path) - return download(uri, "slackbuilds/#{tarball}").hexdigest + return SlackBuilder.download(uri, "slackbuilds/#{tarball}").hexdigest end - checksum = download uri, "slackbuilds/#{tarball}" + checksum = SlackBuilder.download uri, "slackbuilds/#{tarball}" sh(*upload_command(tarball, remote_path)) checksum.hexdigest end |
