Class: Geoblacklight::Download

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Download.



5
6
7
8
9
10
11
# File 'lib/geoblacklight/download.rb', line 5

def initialize(document, options = {})
  Geoblacklight.deprecation.warn(
    "#{self.class} is deprecated; GeoBlacklight 6 removes the generated download subsystem: downloads are direct links only, rendered by Geoblacklight::Document::DownloadLinksComponent"
  )
  @document = document
  @options = options
end

Class Method Details

.file_pathObject



21
22
23
# File 'lib/geoblacklight/download.rb', line 21

def self.file_path
  Settings.DOWNLOAD_PATH || Rails.root.join("tmp", "cache", "downloads")
end

Instance Method Details

#create_download_fileString

Creates temporary file on file system and renames it if download completes successfully. Will raise and rescue if the wrong file format is downloaded

Returns:

  • (String)

    filename of the completed download



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/geoblacklight/download.rb', line 45

def create_download_file
  download = initiate_download

  File.open("#{file_path_and_name}.tmp", "wb") do |file|
    fail Geoblacklight::Exceptions::WrongDownloadFormat unless matches_mimetype?(download)
    file.write download.body
  end
  File.rename("#{file_path_and_name}.tmp", file_path_and_name)
  file_name
rescue Geoblacklight::Exceptions::WrongDownloadFormat => error
  Geoblacklight.logger.error "#{error} expected #{@options[:content_type]} " \
                             "received #{download.headers["content-type"]}"
  File.delete("#{file_path_and_name}.tmp")
  raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: "Wrong download type"
end

#download_exists?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/geoblacklight/download.rb', line 29

def download_exists?
  File.file?(file_path_and_name)
end

#downloadable?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/geoblacklight/download.rb', line 13

def downloadable?
  @document.downloadable?
end

#file_nameObject



17
18
19
# File 'lib/geoblacklight/download.rb', line 17

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

#file_path_and_nameObject



25
26
27
# File 'lib/geoblacklight/download.rb', line 25

def file_path_and_name
  "#{self.class.file_path}/#{file_name}"
end

#getObject



33
34
35
36
37
38
39
# File 'lib/geoblacklight/download.rb', line 33

def get
  if download_exists?
    file_name
  else
    create_download_file
  end
end

#initiate_downloadFaraday::Request

Initiates download from a remote source url using the request_params. Will catch Faraday::ConnectionFailed and Faraday::TimeoutError

Returns:

  • (Faraday::Request)

    returns a Faraday::Request object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/geoblacklight/download.rb', line 66

def initiate_download
  conn = Faraday.new(url: url)
  conn.get do |request|
    request.params = @options[:request_params]
    request.options.timeout = timeout
    request.options.open_timeout = timeout
  end
rescue Faraday::ConnectionFailed
  raise Geoblacklight::Exceptions::ExternalDownloadFailed,
    message: "Download connection failed",
    url: conn.url_prefix.to_s
rescue Faraday::TimeoutError
  raise Geoblacklight::Exceptions::ExternalDownloadFailed,
    message: "Download timed out",
    url: conn.url_prefix.to_s
end

#url_with_paramsString

Creates a download url for the object

Returns:

  • (String)


86
87
88
# File 'lib/geoblacklight/download.rb', line 86

def url_with_params
  url + "/?" + URI.encode_www_form(@options[:request_params])
end