Sha256: c3f7a485b5924abada5613d5261e3788e05a7fae1b692d047e35bdf337c215cf
Contents?: true
Size: 1.31 KB
Versions: 1
Compression:
Stored size: 1.31 KB
Contents
# frozen_string_literal: true require "digest" require "fileutils" require "uri" module Miteru class Downloader attr_reader :base_dir, :memo def initialize(base_dir = "/tmp") @base_dir = base_dir @memo = {} raise ArgumentError, "#{base_dir} doesn't exist." unless Dir.exist?(base_dir) end def download_kits(kits) kits.each { |kit| download_kit kit } end private def download_kit(kit) destination = kit.filepath_to_download begin downloaded_as = HTTPClient.download(kit.url, destination) hash = sha256(downloaded_as) # Remove a downloaded file if it is not unique if duplicated?(hash) puts "Don't download #{kit.url}. The same hash is already recorded. (SHA256: #{hash})." FileUtils.rm downloaded_as return end # Record a kit in DB Record.create_by_kit_and_hash(kit, hash) puts "Download #{kit.url} as #{downloaded_as}" rescue Down::Error => e puts "Failed to download: #{kit.url} (#{e})" end end def sha256(path) return memo[path] if memo.key?(path) digest = Digest::SHA256.file(path) hash = digest.hexdigest memo[path] = hash hash end def duplicated?(hash) !Record.unique_hash?(hash) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
miteru-1.0.0 | lib/miteru/downloader.rb |