Sha256: 88d3f1cb61d4142c2c9ebd0a90fcdd4f3f81b79ae668839aac0c08f199e450ed

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module SolrWrapper
  class ChecksumValidator
    attr_reader :config

    ALGORITHM = 'sha1'

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solr_wrapper-2.0.0 lib/solr_wrapper/checksum_validator.rb