Module: CommonwealthVlrEngine::DownloadsHelperBehavior

Included in:
DownloadsHelper
Defined in:
app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb

Instance Method Summary collapse

Instance Method Details

create an array of download links images have to be handled by a separate function since there are multiple sizes



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 6

def create_download_links(document, files_hash)
  download_links = []
  non_img_file_types = [files_hash[:audio], files_hash[:documents], files_hash[:ereader], files_hash[:generic]]
  if has_downloadable_images?(document, files_hash) && !files_hash[:images].empty?
    download_links.concat(image_download_links(document, files_hash[:images]))
  end
  non_img_file_types.each do |file_type|
    file_type.each do |file|
      object_profile_json = JSON.parse(file['object_profile_ssm'].first)
      download_links << file_download_link(file['id'],
                                           download_link_title(document, object_profile_json),
                                           object_profile_json,
                                           'productionMaster',
                                           download_link_options)
    end
  end
  download_links
end


25
26
27
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 25

def download_link_class
  'sidebar_downloads_link'
end


29
30
31
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 29

def download_link_options
  {class: download_link_class, rel: 'nofollow', data: {:ajax_modal => 'trigger'}}
end


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 33

def download_link_title(document, object_profile, datastream_id=nil)
  if !object_profile || (document[:has_model_ssim].include? "info:fedora/afmodel:Bplmodels_ImageFile")
    link_title = t("blacklight.downloads.images.#{datastream_id}")
  else
    file_name_ext = object_profile["objLabel"].split('.')
    if document[:identifier_ia_id_ssi] || (document[:active_fedora_model_ssi] == "Bplmodels::EreaderFile")
      link_title = ia_download_title(file_name_ext[1])
    else
      link_title = file_name_ext[0]
    end
  end
  link_title
end


113
114
115
116
117
118
119
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 113

def file_download_link(object_pid, link_title, object_profile_json, datastream_id, link_options={})
  link_to(link_title,
          download_path(object_pid, datastream_id: datastream_id),
          link_options) + (:span,
                                      "(#{file_type_string(datastream_id, object_profile_json)}, #{file_size_string(datastream_id, object_profile_json)})",
                                      class: 'download_info')
end

#file_size_string(datastream_id, object_profile_json) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 143

def file_size_string(datastream_id, object_profile_json)
  if object_profile_json
    if datastream_id == 'accessFull'
      file_size_string = '~' +
          number_to_human_size((object_profile_json["datastreams"]["productionMaster"]["dsSize"] * 0.083969078))
    else
      file_size_string = number_to_human_size(object_profile_json["datastreams"][datastream_id]["dsSize"])
      file_size_string.insert(0,'~') if object_profile_json["zip"]
    end
  else
    file_size_string = 'multi-file ZIP'
  end
  file_size_string
end

#file_type_string(datastream_id, object_profile_json) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 121

def file_type_string(datastream_id, object_profile_json)
  if object_profile_json
    if datastream_id == 'accessFull' || datastream_id == 'access800'
      file_type_string = 'JPEG'
    else
      #file_type_string = object_profile_json["datastreams"][datastream_id]["dsMIME"].split('/')[1].upcase
      file_type_string = object_profile_json["objLabel"].split('.')[1].upcase
    end
    file_type_string << ', multi-file ZIP' if object_profile_json["zip"]
  else
    file_type_string = case datastream_id
                         when 'productionMaster'
                           'TIF'
                         when 'JPEG2000'
                           datastream_id
                         else
                           'JPEG'
                       end
  end
  file_type_string
end

#has_downloadable_files?(document, files_hash) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 47

def has_downloadable_files?(document, files_hash)
  has_downloadable_images?(document, files_hash) ||
      files_hash[:documents].present? ||
      files_hash[:audio].present? ||
      files_hash[:generic].present? ||
      files_hash[:ereader].present?
end

#has_downloadable_images?(document, files_hash) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 55

def has_downloadable_images?(document, files_hash)
  has_image_files?(files_hash) && license_allows_download?(document)
end

#ia_download_title(file_extension) ⇒ Object

render the file type names for Internet Archive book item download links



60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 60

def ia_download_title(file_extension)
  case file_extension
    when 'mobi'
      'Kindle'
    when 'zip'
      'Daisy'
    else
      file_extension.upcase
  end
end

#image_datastreams(object_profile_json) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 71

def image_datastreams(object_profile_json)
  image_datastreams = []
  stored_datastreams = %w(productionMaster access800 georectifiedMaster)
  stored_datastreams.each do |datastream_id|
    image_datastreams << datastream_id if object_profile_json["datastreams"][datastream_id]
  end
  image_datastreams.insert(1, 'accessFull')
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 80

def image_download_links(document, image_files_hash)
  if document[:identifier_ia_id_ssi]
    [file_download_link(document[:id],
                        t("blacklight.downloads.images.accessFull"),
                        nil,
                        'JPEG2000',
                        download_link_options)]
  else
    object_profile_json = JSON.parse(image_files_hash.first['object_profile_ssm'].first)
    image_links = []
    image_datastreams(object_profile_json).each do |datastream_id|
      if image_files_hash.length == 1
        object_profile = object_profile_json
        object_id = image_files_hash.first['id']
      else
        object_profile = setup_zip_object_profile(image_files_hash, datastream_id)
        object_id = document[:id]
      end
      image_links << file_download_link(object_id,
                                        t("blacklight.downloads.images.#{datastream_id}"),
                                        object_profile,
                                        datastream_id,
                                        download_link_options)
    end
    image_links
  end
end

#license_allows_download?(document) ⇒ Boolean

parse the license statement and return true if image downloads are allowed

Returns:

  • (Boolean)


109
110
111
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 109

def license_allows_download? document
  document[:license_ssm].to_s =~ /(Creative Commons|No known restrictions)/
end

#setup_zip_object_profile(image_files_hash, datastream_id) ⇒ Object

create a composite object_profile_json object from multiple file objects used to display size of ZIP archive



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 160

def setup_zip_object_profile(image_files_hash, datastream_id)
  datastream_id_to_use = datastream_id == 'accessFull' ? 'productionMaster' : datastream_id
  object_profile = {zip: true,
                    objLabel: datastream_id == 'productionMaster' ? '.TIF' : '.JPEG',
                    datastreams: {datastream_id_to_use.to_sym => {}}}
  zip_size = 0
  image_files_hash.each do |image_file|
    img_object_profile_json = JSON.parse(image_file['object_profile_ssm'].first)
    zip_size += img_object_profile_json["datastreams"][datastream_id_to_use]["dsSize"]
  end
  # estimate compression, pretty rough
  zip_size = case datastream_id
               when 'productionMaster'
                 zip_size * 0.798
               when 'accessFull'
                 zip_size * 0.839
               else
                 zip_size * 0.927
             end
  object_profile[:datastreams][datastream_id_to_use.to_sym][:dsSize] = zip_size
  object_profile.deep_stringify_keys
end

#url_for_download(document, datastream_id) ⇒ Object



183
184
185
186
187
188
189
# File 'app/helpers/commonwealth_vlr_engine/downloads_helper_behavior.rb', line 183

def url_for_download(document, datastream_id)
  if document[:identifier_ia_id_ssi] && datastream_id == 'JPEG2000'
    "https://archive.org/download/#{document[:identifier_ia_id_ssi]}/#{document[:identifier_ia_id_ssi]}_jp2.zip"
  else
    trigger_downloads_path(document.id, datastream_id: datastream_id)
  end
end