Module: Sufia::GenericFile

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
Audit, Characterization, Export, Permissions, Thumbnail, ModelMethods, Noid
Included in:
GenericFile
Defined in:
lib/sufia/generic_file.rb,
lib/sufia/generic_file/audit.rb,
lib/sufia/generic_file/export.rb,
lib/sufia/generic_file/actions.rb,
lib/sufia/generic_file/thumbnail.rb,
lib/sufia/generic_file/permissions.rb,
lib/sufia/generic_file/characterization.rb

Defined Under Namespace

Modules: Actions, Audit, Characterization, Export, Permissions, Thumbnail

Constant Summary

Constants included from Audit

Audit::NO_RUNS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Permissions

#paranoid_permissions, #permissions=, #set_visibility

Methods included from Audit

#audit, #audit!, #audit_stat, #audit_stat!, #logs, #per_version

Methods included from Characterization

#append_metadata, #characterization_terms, #characterize, #characterize_if_changed

Methods included from Export

#export_as_apa_citation, #export_as_chicago_citation, #export_as_endnote, #export_as_mla_citation, #export_as_openurl_ctx_kev

Methods included from Thumbnail

#create_thumbnail

Methods included from Noid

namespaceize, #noid, noidify, #normalize_identifier

Methods included from ModelMethods

#apply_depositor_metadata

Class Method Details

.noid_indexerObject

Unstemmed, searchable, stored



137
138
139
# File 'lib/sufia/generic_file.rb', line 137

def self.noid_indexer
  @noid_indexer ||= Solrizer::Descriptor.new(:text, :indexed, :stored)
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/sufia/generic_file.rb', line 77

def audio?
  # audio/x-wave is the mime type that fits 0.6.0 returns for a wav file.
  # audio/mpeg is the mime type that fits 0.6.0 returns for an mp3 file.
  ['audio/mp3', 'audio/mpeg', 'audio/x-wave', 'audio/x-wav', 'audio/ogg'].include? self.mime_type
end

#cleanup_trophiesObject



119
120
121
# File 'lib/sufia/generic_file.rb', line 119

def cleanup_trophies
  Trophy.destroy_all(generic_file_id: self.noid)
end

#deleteObject



43
44
45
46
# File 'lib/sufia/generic_file.rb', line 43

def delete
   self.cleanup_trophies
   super
end

#file_formatObject



150
151
152
153
154
155
# File 'lib/sufia/generic_file.rb', line 150

def file_format
  return nil if self.mime_type.blank? and self.format_label.blank?
  return self.mime_type.split('/')[1]+ " ("+self.format_label.join(", ")+")" unless self.mime_type.blank? or self.format_label.blank?
  return self.mime_type.split('/')[1] unless self.mime_type.blank?
  return self.format_label
end

#image?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/sufia/generic_file.rb', line 69

def image?
  ['image/png','image/jpeg', 'image/jpg', 'image/jp2', 'image/bmp', 'image/gif'].include? self.mime_type
end

#label=(new_label) ⇒ Object



162
163
164
165
166
167
# File 'lib/sufia/generic_file.rb', line 162

def label=(new_label)
  @inner_object.label = new_label
  if self.title.empty?
    self.title = new_label
  end
end

#pdf?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sufia/generic_file.rb', line 65

def pdf?
  ['application/pdf'].include? self.mime_type
end

#persistent_urlObject



83
84
85
# File 'lib/sufia/generic_file.rb', line 83

def persistent_url
  "#{Sufia::Engine.config.persistent_hostpath}#{noid}"
end

#processing?Boolean

Is this file in the middle of being processed by a batch?

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/sufia/generic_file.rb', line 190

def processing?
   return false if self.batch.blank?
   return false if !self.batch.methods.include? :status
   return (!self.batch.status.empty?) && (self.batch.status.count == 1) && (self.batch.status[0] == "processing")
end

#record_version_committer(user) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/sufia/generic_file.rb', line 55

def record_version_committer(user)
  version = content.latest_version
  # content datastream not (yet?) present
  return if version.nil?
  VersionCommitter.create(:obj_id => version.pid,
                          :datastream_id => version.dsid,
                          :version_id => version.versionID,
                          :committer_login => user.user_key)
end


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sufia/generic_file.rb', line 124

def related_files
  relateds = begin
               self.batch.generic_files
             rescue NoMethodError => e
               #batch is nil - When would this ever happen?
               batch_id = self.object_relations["isPartOf"].first || self.object_relations[:is_part_of].first
               return [] if batch_id.nil?
               self.class.find(Solrizer.solr_name('is_part_of', :symbol) => batch_id)
             end
  relateds.reject { |gf| gf.pid == self.pid }
end

#remove_blank_assertionsObject



48
49
50
51
52
# File 'lib/sufia/generic_file.rb', line 48

def remove_blank_assertions
  terms_for_editing.each do |key|
    self[key] = nil if self[key] == ['']
  end
end

#retry_warmingObject



87
88
89
90
91
92
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
# File 'lib/sufia/generic_file.rb', line 87

def retry_warming
    save_tries = 0
    conflict_tries = 0
    begin
      yield
    rescue RSolr::Error::Http => error
      save_tries += 1
      logger.warn "Retry Solr caught RSOLR error on #{self.pid}: #{error.inspect}"
      # fail for good if the tries is greater than 3
      raise if save_tries >=3
      sleep 0.01
      retry      
    rescue  ActiveResource::ResourceConflict => error
      conflict_tries += 1
      logger.warn "Retry caught Active Resource Conflict #{self.pid}: #{error.inspect}"
      raise if conflict_tries >=10
      sleep 0.01
      retry
    rescue =>error
      if (error.to_s.downcase.include? "conflict")
        conflict_tries += 1
        logger.warn "Retry caught Active Resource Conflict #{self.pid}: #{error.inspect}"
        raise if conflict_tries >=10
        sleep 0.01
        retry
      else
        raise
      end          
    
    end
end

#terms_for_displayObject



185
186
187
# File 'lib/sufia/generic_file.rb', line 185

def terms_for_display
  self..class.config.keys
end

#terms_for_editingObject



180
181
182
183
# File 'lib/sufia/generic_file.rb', line 180

def terms_for_editing
  terms_for_display -
   [:part_of, :date_modified, :date_uploaded, :format] #, :resource_type]
end

#to_jq_uploadObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/sufia/generic_file.rb', line 169

def to_jq_upload
  return {
    "name" => self.title,
    "size" => self.file_size,
    "url" => "/files/#{noid}",
    "thumbnail_url" => self.pid,
    "delete_url" => "deleteme", # generic_file_path(:id => id),
    "delete_type" => "DELETE"
  }
end

#to_paramObject

Redefine this for more intuitive keys in Redis



158
159
160
# File 'lib/sufia/generic_file.rb', line 158

def to_param
  noid
end

#to_solr(solr_doc = {}, opts = {}) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/sufia/generic_file.rb', line 141

def to_solr(solr_doc={}, opts={})
  super(solr_doc, opts)
  solr_doc[Solrizer.solr_name('label')] = self.label
  solr_doc[Solrizer.solr_name('noid', Sufia::GenericFile.noid_indexer)] = noid
  solr_doc[Solrizer.solr_name('file_format')] = file_format
  solr_doc[Solrizer.solr_name('file_format', :facetable)] = file_format
  return solr_doc
end

#video?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/sufia/generic_file.rb', line 73

def video?
  ['video/mpeg', 'video/mp4', 'video/webm', 'video/x-msvideo', 'video/avi', 'video/quicktime', 'application/mxf'].include? self.mime_type
end