Sha256: 9e24d2e4c6d7e7d97d55c47af1958d48490521cf28396300af34c5017b3410f5

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module SolrWrapper
  class ChecksumValidator
    attr_reader :config

    def initialize(config)
      @config = config
    end

    def clean!
      path = checksum_path(algorithm)
      FileUtils.remove_entry(path) if File.exist? path
    end

    def validate?(file)
      return true if config.validate == false
      Digest.const_get(algorithm.upcase).file(file).hexdigest == expected_sum(algorithm)
    end

    def validate!(file)
      unless validate? file
        raise "Checksum mismatch" unless config.ignore_checksum
      end
    end

    private

      def checksumurl(suffix)
        if config.default_download_url == config.static_config.archive_download_url
          "#{config.default_download_url}.#{suffix}"
        else
          "http://www.us.apache.org/dist/lucene/solr/#{config.static_config.version}/solr-#{config.static_config.version}.zip.#{suffix}"
        end
      end

      def checksum_path(suffix)
        File.join(config.download_dir, File.basename(checksumurl(suffix)))
      end

      def expected_sum(alg)
        config.checksum || read_file(alg)
      end

      def read_file(alg)
        open(checksumfile(alg)).read.split(" ").first
      end

      def checksumfile(alg)
        path = checksum_path(alg)
        unless File.exist? path
          Downloader.fetch_with_progressbar checksumurl(alg), path
        end
        path
      end

      def algorithm
        return config.static_config.algorithm if config.static_config.algorithm
        return 'sha1' if config.static_config.version =~ /^[1-6]/ || config.static_config.version =~ /^[7]\.[0-4]/

        'sha512'
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solr_wrapper-3.0.1 lib/solr_wrapper/checksum_validator.rb
solr_wrapper-3.0.0 lib/solr_wrapper/checksum_validator.rb
solr_wrapper-2.2.0 lib/solr_wrapper/checksum_validator.rb
solr_wrapper-2.1.0 lib/solr_wrapper/checksum_validator.rb