Sha256: 1ffa3b5aace7e8d867f97825d0af373975d67bfaea27d790bfea44cccbd0d7fb

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

class Download
  def initialize(document, options = {})
    @document = document
    @options = options
  end

  def downloadable?
    @document.downloadable?
  end

  def file_name
    "#{@document[:layer_slug_s]}-#{@options[:type]}.#{@options[:extension]}"
  end

  def file_path
    "#{Rails.root}/tmp/cache/downloads/#{file_name}"
  end

  def download_exists?
    File.file?(file_path)
  end

  def get
    if download_exists?
      file_name
    else
      create_download_file
    end
  end

  def create_download_file
    download = initiate_download
    File.open("#{file_path}.tmp", 'wb')  do |file|
      if download.headers['content-type'] == @options[:content_type]
        file.write download.body
      else
        fail Geoblacklight::Exceptions::WrongDownloadFormat
      end
    end
    File.rename("#{file_path}.tmp", file_path)
    file_name
  rescue Geoblacklight::Exceptions::WrongDownloadFormat => error
    Geoblacklight.logger.error "#{error} expected #{@options[:content_type]} received #{download.headers['content-type']}"
    File.delete("#{file_path}.tmp")
    nil
  end

  def initiate_download
    url = @document.references.send(@options[:service_type]).endpoint
    url += '/reflect' if @options[:reflect]
    conn = Faraday.new(url: url)
    conn.get do |request|
      request.params = @options[:request_params]
      request.options = {
        timeout: 16,
        open_timeout: 16
      }
    end
  rescue Faraday::Error::ConnectionFailed => error
    Geoblacklight.logger.error error
    nil
  rescue Faraday::Error::TimeoutError => error
    Geoblacklight.logger.error error
    nil
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geoblacklight-0.3.3 lib/geoblacklight/download.rb
geoblacklight-0.3.2 lib/geoblacklight/download.rb
geoblacklight-0.3.1 lib/geoblacklight/download.rb
geoblacklight-0.3.0 lib/geoblacklight/download.rb
geoblacklight-0.2.2 lib/geoblacklight/download.rb
geoblacklight-0.2.1 lib/geoblacklight/download.rb
geoblacklight-0.2.0 lib/geoblacklight/download.rb