Class: Bplmodels::File

Inherits:
ActiveFedora::Base
  • Object
show all
Includes:
ActiveFedora::Auditable, Characterization, Hydra::AccessControls::Permissions, Hydra::Derivatives, Hydra::ModelMethods
Defined in:
app/models/bplmodels/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Characterization

#append_metadata, #characterization_terms, #characterize, #duration, #fits_config_values, #height, #sample_rate, #width

Class Method Details

.mint(args) ⇒ Object

Expects the following args: parent_pid => id of the parent object local_id => local ID of the object local_id_type => type of that local ID label => label of the collection institution_pid => instituional access of this file



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/models/bplmodels/file.rb', line 236

def self.mint(args)

  #TODO: Duplication check here to prevent over-writes?

  args[:namespace_id] ||= ARK_CONFIG_GLOBAL['namespace_commonwealth_pid']

  response = Typhoeus::Request.post(ARK_CONFIG_GLOBAL['url'] + "/arks.json", :params => {:ark=>{:parent_pid=>args[:parent_pid], :namespace_ark => ARK_CONFIG_GLOBAL['namespace_commonwealth_ark'], :namespace_id=>args[:namespace_id], :url_base => ARK_CONFIG_GLOBAL['ark_commonwealth_base'], :model_type => self.name, :local_original_identifier=>args[:local_id], :local_original_identifier_type=>args[:local_id_type]}})
  begin
    as_json = JSON.parse(response.body)
  rescue => ex
    raise('Error in JSON response for minting a file pid.')
  end

  #Below stopped working suddenly?
=begin
  dup_check = ActiveFedora::Base.find(:pid=>as_json["pid"])
  if dup_check.present?
    return as_json["pid"]
  end
=end


  Bplmodels::File.find_in_batches('id'=>as_json["pid"]) do |group|
    group.each { |solr_result|
      return as_json["pid"]
    }
  end

  object = self.new(:pid=>as_json["pid"])

  object.label = args[:label]

  object.read_groups = ["public"]
  object.edit_groups = ["superuser", "admin[#{args[:institution_pid]}]"]  if args[:institution_pid].present?

  object..item_ark_info.ark_id = args[:local_id]
  object..item_ark_info.ark_type = args[:local_id_type]
  object..item_ark_info.ark_parent_pid = args[:parent_pid]

  return object
end

Instance Method Details

#apply_default_permissionsObject



46
47
48
49
# File 'app/models/bplmodels/file.rb', line 46

def apply_default_permissions
  self.datastreams["rightsMetadata"].update_permissions( "group"=>{"Repository Administrators"=>"edit"} )
  self.save
end

#cache_invalidateObject



278
279
280
281
282
283
284
285
286
287
# File 'app/models/bplmodels/file.rb', line 278

def cache_invalidate
  response = Typhoeus::Request.post(DERIVATIVE_CONFIG_GLOBAL['url'] + "/processor/objectcacheinvalidation.json", :params => {:file_pid=>self.pid, :environment=>Bplmodels.environment})
  as_json = JSON.parse(response.body)

  if as_json['result'] == "false"
    raise "Error Deleting the Cache! Server error!"
  end

  return true
end

#deleteObject



38
39
40
41
42
43
44
# File 'app/models/bplmodels/file.rb', line 38

def delete
  #FIXME: This should just not be hardcoded in AVI Processor
  if ActiveFedora.config.credentials[:url].match('fedora.digitalcommonwealth.org/fedora')
    #cache_invalidate
  end
  super()
end

#derivative_service(is_new) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'app/models/bplmodels/file.rb', line 217

def derivative_service(is_new)
  response = Typhoeus::Request.post(DERIVATIVE_CONFIG_GLOBAL['url'] + "/processor/byfile.json", :params => {:pid=>self.pid, :new=>is_new, :environment=>Bplmodels.environment})
  puts response.body.to_s
  as_json = JSON.parse(response.body)

  if as_json['result'] == "false"
    raise "Error Generating Derivatives For Object: " + pid
  end

  return true
end

#generate_derivativesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/bplmodels/file.rb', line 93

def generate_derivatives
  #Sample object on prod: https://fedora.digitalcommonwealth.org/fedora/objects/commonwealth:9w032896b/datastreams/productionMaster/content
  #Sample object from test book object: https://fedoratest.bpl.org/fedora/objects/bpl-test:rf55zp77f/datastreams/productionMaster/content
  case self.productionMaster.mimeType
    when 'application/pdf'
      #transform_datastream :productionMaster, { :thumb => "100x100>" }

      pdffile = Tempfile.new(['derivative','.pdf'])
      pdffile.binmode
      pdffile.write(self.productionMaster.content) #ActiveFedora.config.credentials[:url] + '/objects/' + self.pid + '/datastreams/productionMaster/content'
      pdffile.close

      current_page = 0
      total_colors = 0
      until total_colors > 1 do
        #Won't work... asks for login with the brackets...
        img = Magick::Image.read(pdffile.path + '[' + current_page.to_s + ']'){
          self.quality = 100
          self.density = 200
        }.first
        total_colors = img.total_colors
        current_page = current_page + 1
      end

      #This is horrible. But if you don't do this, some PDF files won't come out right at all.
      #Multiple attempts have failed to fix this but perhaps the bug will be patched in ImageMagick.
      #To duplicate, one can use the PDF files at: http://libspace.uml.edu/omeka/files/original/7ecb4dc9579b11e2b53ccc2040e58d36.pdf
      img = Magick::Image.from_blob( img.to_blob { self.format = "jpg" } ).first

      thumb = img.resize_to_fit(300,300)

      self.thumbnail300.content = thumb.to_blob { self.format = "jpg" }
      self.thumbnail300.mimeType = 'image/jpeg'
      self.thumbnail300.dsLabel = self.productionMaster.label

      pdffile.delete
    when 'audio/wav'
      #transform_datastream :productionMaster, { :mp3 => {format: 'mp3'}, :ogg => {format: 'ogg'} }, processor: :audio
    when 'video/avi'
      #transform_datastream :productionMaster, { :mp4 => {format: 'mp4'}, :webm => {format: 'webm'} }, processor: :video
    when 'image/tiff', 'image/png', 'image/jpg', 'image/png'
      begin
        transform_datastream :productionMaster, { :testJP2k => { recipe: :default, datastream: 'accessMaster'  } }, processor: 'jpeg2k_image'
      rescue => error
        # First one is from Blue Books collection. Second one is from commonwealth:xd07m887b
        if error.message.include?('compressed TIFF files') || error.message.include?("The number of colours associated with the colour space specified using")
=begin
          jp2_img = MiniMagick::Image.read(self.productionMaster.content) do |b|
            b.format "jp2"
          end
=end

          jp2_img = MiniMagick::Image.read(self.productionMaster.content)
          jp2_img.format 'jp2'
          self.accessMaster.content = jp2_img.to_blob
          self.accessMaster.mimeType = 'image/jp2'
          jp2_img.destroy!
        else
          raise error
        end
      end
      transform_datastream :productionMaster, { :thumb => {size: "300x300>", datastream: 'thumbnail300', format: 'jpg'} }
      transform_datastream :productionMaster, { :thumb => {size: "x800>", datastream: 'access800', format: 'jpg'} }
      self.accessMaster.dsLabel = self.productionMaster.label
      self.thumbnail300.dsLabel = self.productionMaster.label
      self.access800.dsLabel = self.productionMaster.label
    when 'image/jpeg' #FIXME
=begin
      jp2_img = MiniMagick::Image.read(self.productionMaster.content) do |b|
        b.format "jp2"
      end
=end

      jp2_img = MiniMagick::Image.read(self.productionMaster.content)
      jp2_img.format 'jp2'

      self.accessMaster.content = jp2_img.to_blob
      self.accessMaster.mimeType = 'image/jp2'
      jp2_img.destroy!

      transform_datastream :productionMaster, { :thumb => {size: "300x300>", datastream: 'thumbnail300', format: 'jpg'} }
      transform_datastream :productionMaster, { :thumb => {size: "x800>", datastream: 'access800', format: 'jpg'} }
      self.accessMaster.dsLabel = self.productionMaster.label
      self.thumbnail300.dsLabel = self.productionMaster.label
      self.access800.dsLabel = self.productionMaster.label
    when 'image/jp2'
      self.accessMaster.content = self.productionMaster.content
      self.accessMaster.mimeType = 'image/jp2'


=begin
      source_path = "#{ActiveFedora.config.credentials[:url]}/#{self.productionMaster.url}"
      image = MiniMagick::Image.open(source_path)
      quality = image['%[channels]'] == 'gray' ? 'gray' : 'color'
      long_dim = Hydra::Derivatives::Jpeg2kImage.long_dim(image)
      directives = { recipe: :default, datastream: 'accessMaster'  }
      recipe = Hydra::Derivatives::Jpeg2kImage.kdu_compress_recipe(directives, quality, long_dim)

      file_path = Hydra::Derivatives::Jpeg2kImage.tmp_file('.jp2')
      image.write file_path


      output_file = Hydra::Derivatives::Jpeg2kImage.tmp_file('.jp2')
      Hydra::Derivatives::Jpeg2kImage.encode(file_path, recipe, output_file)
      self.accessMaster.content = ::File.open(output_file)
      self.accessMaster.mimeType = 'image/jp2'
      #End wonky code

      #TODO: Move this to a function in Hydra Derivatives...
      self.save
      image.destroy!
      File.unlink(output_file)
      File.unlink(file_path)
=end


      transform_datastream :productionMaster, { :thumb => {size: "300x300>", datastream: 'thumbnail300', format: 'jpg'} }
      transform_datastream :productionMaster, { :thumb => {size: "x800>", datastream: 'access800', format: 'jpg'} }
      self.accessMaster.dsLabel = self.productionMaster.label
      self.thumbnail300.dsLabel = self.productionMaster.label
      self.access800.dsLabel = self.productionMaster.label


  end

end

#saveObject



51
52
53
54
# File 'app/models/bplmodels/file.rb', line 51

def save
  self.add_relationship(:has_model, "info:fedora/afmodel:Bplmodels_File")
  super()
end

#to_solr(doc = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/bplmodels/file.rb', line 56

def to_solr(doc = {} )
  doc = super(doc)
  if self..marked_for_deletion.present?
    doc['marked_for_deletion_bsi']  =  self..marked_for_deletion.first
    doc['marked_for_deletion_reason_ssi']  =  self..marked_for_deletion.reason.first
  end

  if ['image/tiff', 'image/png', 'image/jpg', 'image/jp2'].include?(self.productionMaster.mimeType) and self.accessMaster.blank?
    doc['derivative_processsed_ssi'] = 'false'
  else
    doc['derivative_processsed_ssi'] = 'true'
  end

  if self.ocrMaster.present?
    doc['ocr_tsiv'] = Bplmodels::DatastreamInputFuncs.strip_value(self.ocrMaster.content.squish)
    doc['has_ocr_text_bsi'] = true
  end

  if self..present?
    doc['page_type_ssi'] = self..pageData.page.page_type.first
    doc['hand_side_ssi'] = self..pageData.page.hand_side.first
    doc['page_num_label_ssi'] = self..pageData.page.page_number.first if self..pageData.page.page_number.present?
    doc['page_num_label_type_ssi'] = self..pageData.page.page_number.sequence.first if self..pageData.page.page_number.present?
    #doc['has_ocr_master_ssi'] = self.bookMetadata.book.page_data.page.has_ocrMaster.first
    #doc['has_djvu_json_ssi'] = self.bookMetadata.book.page_data.page.has_djvu.first

    if self.djvuCoords.present?
      doc['has_djvu_json_ssi'] = 'true'
    end
  end

  doc['checksum_file_md5_ssi'] = self.productionMaster.checksum

  doc

end