lib/fontist/downloader.rb in fontist-0.4.0 vs lib/fontist/downloader.rb in fontist-1.0.0

- old
+ new

@@ -1,28 +1,25 @@ -require "down" -require "digest" - module Fontist class Downloader def initialize(file, file_size: nil, sha: nil, progress: nil) - @sha = sha @file = file @progress = progress + @sha = [sha].flatten.compact @file_size = (file_size || default_file_size).to_i end def download file = download_file - verify_file_checksum(file) || raise_invalid_file - end - def verify_file_checksum(file) - file if Digest::SHA256.file(file) === sha - end + if !sha.empty? && !sha.include?(Digest::SHA256.file(file).to_s) + raise(Fontist::Errors::TemparedFileError.new( + "The downloaded file from #{@file} doesn't " \ + "match with the expected sha256 checksum!" + )) + end - def raise_invalid_file - raise(Fontist::Errors::TemparedFileError) + file end def self.download(file, options = {}) new(file, options).download end @@ -50,9 +47,12 @@ @file, progress_proc: -> (progress) { bar.increment(progress / byte_to_megabyte) if @progress === true } ) + + rescue Down::NotFound + raise(Fontist::Errors::InvalidResourceError.new("Invalid URL: #{@file}")) end end class ProgressBar def initialize(total)