Class: Download

Inherits:
Object
  • Object
show all
Defined in:
lib/geoblacklight/download.rb

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Download

Returns a new instance of Download.


2
3
4
5
# File 'lib/geoblacklight/download.rb', line 2

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

Instance Method Details

#create_download_fileObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/geoblacklight/download.rb', line 31

def create_download_file
  download = initiate_download
  unless download.present?
    raise Geoblacklight::Exceptions::ExternalDownloadFailed
  end
  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::ExternalDownloadFailed
  Geoblacklight.logger.error 'Download from external server failed'
  nil
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

#download_exists?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/geoblacklight/download.rb', line 19

def download_exists?
  File.file?(file_path)
end

#downloadable?Boolean

Returns:

  • (Boolean)

7
8
9
# File 'lib/geoblacklight/download.rb', line 7

def downloadable?
  @document.downloadable?
end

#file_nameObject


11
12
13
# File 'lib/geoblacklight/download.rb', line 11

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

#file_pathObject


15
16
17
# File 'lib/geoblacklight/download.rb', line 15

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

#getObject


23
24
25
26
27
28
29
# File 'lib/geoblacklight/download.rb', line 23

def get
  if download_exists?
    file_name
  else
    create_download_file
  end
end

#initiate_downloadObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geoblacklight/download.rb', line 54

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: Settings.TIMEOUT_DOWNLOAD,
      open_timeout: Settings.TIMEOUT_DOWNLOAD
    }
  end
rescue Faraday::Error::ConnectionFailed => error
  Geoblacklight.logger.error error.inspect
  nil
rescue Faraday::Error::TimeoutError => error
  Geoblacklight.logger.error error.inspect
  nil
end