Sha256: 041762ce15282c27be8252b8f588fa98b8e116e5efdebb3d54ccade82c770b13
Contents?: true
Size: 1.69 KB
Versions: 3
Compression:
Stored size: 1.69 KB
Contents
# frozen_string_literal: true require "digest" require "fileutils" require "uri" module Miteru class Downloader attr_reader :base_dir def initialize(base_dir = "/tmp") @base_dir = base_dir raise ArgumentError, "#{base_dir} is not existing." unless Dir.exist?(base_dir) end def download_compressed_files(url, compressed_files) compressed_files.each do |path| target_url = "#{url}/#{path}" filename = download_filename(target_url) destination = filepath_to_download(filename) begin download_filepath = HTTPClient.download(target_url, destination) if duplicated?(download_filepath) puts "Do not download #{target_url} because there is a file that has a same hash value in the directory (SHA256: #{sha256(download_filepath)})." FileUtils.rm download_filepath else puts "Download #{target_url} as #{download_filepath}" end rescue Down::Error => e puts "Failed to download: #{target_url} (#{e})" end end end private def download_filename(url) filename = url.split("/").last extname = File.extname(filename) domain = URI(url).hostname "#{domain}_#{filename}_#{SecureRandom.alphanumeric(10)}#{extname}" end def filepath_to_download(filename) "#{base_dir}/#{filename}" end def sha256(path) digest = Digest::SHA256.file(path) digest.hexdigest end def duplicated?(file_path) base = sha256(file_path) sha256s = Dir.glob("#{base_dir}/*.{zip,rar,7z,tar,gz}").map { |path| sha256(path) } sha256s.select { |sha256| sha256 == base }.length > 1 end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
miteru-0.12.0 | lib/miteru/downloader.rb |
miteru-0.11.3 | lib/miteru/downloader.rb |
miteru-0.11.2 | lib/miteru/downloader.rb |