Sha256: 7cd1a8b3957ed1baf8e3c533b3d22e29c1c73ad5d4e02a69c4a6db273d7b804d
Contents?: true
Size: 1.47 KB
Versions: 1
Compression:
Stored size: 1.47 KB
Contents
# frozen_string_literal: true require "cgi" require "securerandom" module Miteru class Kit VALID_EXTENSIONS = Miteru.configuration.valid_extensions attr_reader :url def initialize(url) @url = url end def valid?; valid_ext? && reachable_and_archive_file? end def extname return ".tar.gz" if url.end_with?("tar.gz") File.extname(url) end def basename File.basename(url) end def filename CGI.unescape basename end def download_filepath "#{base_dir}/#{download_filename}" end def filesize return nil unless File.exist?(download_filepath) File.size download_filepath end def filename_with_size return filename unless filesize "#{filename}(#{filesize / 1024}KB)" end private def id @id ||= SecureRandom.hex(10) end def hostname URI(url).hostname end def download_filename "#{hostname}_#{filename}_#{id}#{extname}" end def base_dir @base_dir ||= Miteru.configuration.download_to end def valid_ext? VALID_EXTENSIONS.include? extname end def reachable?(response) response.status.success? end def archive_file?(response) !response.content_type.mime_type.to_s.start_with? "text/" end def reachable_and_archive_file? res = HTTPClient.head(url) reachable?(res) && archive_file?(res) rescue StandardError false end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
miteru-0.14.4 | lib/miteru/kit.rb |