class Gravaty::Utils::Downloader::Downloader
!/usr/bin/env ruby
An HTTP/HTTPS/FTP file downloader library/CLI based upon MiniPortile's HTTP implementation.
- Author
-
Jon Maken
- License
-
3-clause BSD
- Revision
-
2012-03-25 23:01:19 -0600
Constants
- VERSION
Attributes
ftp_data_chunk_size[RW]
logger[RW]
max_ca_verify_depth[RW]
Public Class Methods
download_file(url, full_path, count = 3)
click to toggle source
# File lib/gravaty/utils/downloader.rb, line 63 def self.download_file(url, full_path, count = 3) raise I18n.t('error.nil') if url.nil? return if File.exist?(full_path) http_download(url, full_path, count) end
Private Class Methods
http_download(url, full_path, count)
click to toggle source
# File lib/gravaty/utils/downloader.rb, line 80 def self.http_download(url, full_path, count) uri = URI.parse(url) begin filename = File.basename(uri.path) if ENV['HTTP_PROXY'] # protocol, userinfo, proxy_host, proxy_port = URI.split(ENV['HTTP_PROXY']) proxy_user, proxy_pass = userinfo.split(/:/) if userinfo http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass) else http = Net::HTTP.new(uri.host, uri.port) end if uri.scheme.downcase == 'https' http.use_ssl = true if ENV['SSL_CERT_FILE'] cert_file = ENV['SSL_CERT_FILE'].dup cert_file.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR end if cert_file && File.exist?(cert_file) http.ca_file = cert_file http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.verify_depth = @max_ca_verify_depth else raise <<~HOWTO_MESSAGE To download using HTTPS you must first set the SSL_CERT_FILE environment variable to the path of a valid CA certificate file. A file of bundled public CA certs may be downloaded from: #{' '} http://curl.haxx.se/ca/cacert.pem #{' '} HOWTO_MESSAGE end end http.request_get(uri) do |response| case response when Net::HTTPNotFound output '404 - Not Found' return false when Net::HTTPClientError output "Error: Client Error: #{response.inspect}" return false when Net::HTTPRedirection raise 'Too many redirections, halting.' if count <= 0 url = response['location'] return http_download(url, full_path, count - 1) when Net::HTTPOK temp_file = Tempfile .new("download-#{filename}") temp_file.binmode temp_file << response.body output temp_file.close File.unlink full_path if File.exist?(full_path) FileUtils.mkdir_p File.dirname(full_path) FileUtils.mv temp_file.path, full_path, force: true else puts response end end rescue StandardError => e File.unlink full_path if File.exist?(full_path) output "ERROR: #{e.message}" raise "Failed to download file: #{e.message}" end end
message(text)
click to toggle source
# File lib/gravaty/utils/downloader.rb, line 70 def self.message(text) @logger.print text @logger.flush end
output(text = '')
click to toggle source
# File lib/gravaty/utils/downloader.rb, line 75 def self.output(text = '') @logger.puts text @logger.flush end