Class: GenericResource

Inherits:
ActiveFedora::Base
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveFedora::DatastreamCollections, ActiveFedora::FinderMethods::RepositoryMethods, ActiveFedora::RelsInt, Cul::Hydra::Fedora::UrlHelperBehavior, Cul::Hydra::Models::Common, Cul::Hydra::Models::ImageResource, Hydra::ModelMethods
Defined in:
app/models/generic_resource.rb

Constant Summary collapse

IMAGE_EXT =
{"image/bmp" => 'bmp', "image/gif" => 'gif', "image/jpeg" => 'jpg', "image/png" => 'png', "image/tiff" => 'tif', "image/x-windows-bmp" => 'bmp'}
WIDTH =
RDF::URI(ActiveFedora::Predicates.find_graph_predicate(:image_width))
LENGTH =
RDF::URI(ActiveFedora::Predicates.find_graph_predicate(:image_length))

Constants included from Cul::Hydra::Models::ImageResource

Cul::Hydra::Models::ImageResource::DJATOKA_BASE_URL, Cul::Hydra::Models::ImageResource::DJATOKA_THUMBNAIL_PARMS, Cul::Hydra::Models::ImageResource::EXTENT_PREDICATE, Cul::Hydra::Models::ImageResource::FORMAT_OF_PREDICATE, Cul::Hydra::Models::ImageResource::FORMAT_URI, Cul::Hydra::Models::ImageResource::LENGTH_PREDICATE, Cul::Hydra::Models::ImageResource::WIDTH_PREDICATE

Instance Method Summary collapse

Methods included from Cul::Hydra::Fedora::UrlHelperBehavior

#fedora_ds_url, #fedora_method_url, #fedora_object_url, #fedora_risearch_url, #fedora_url, #pid_for_url

Methods included from Cul::Hydra::Models::ImageResource

#length, #long, #width

Methods included from Cul::Hydra::Models::Common

#cmodel_pid, #datastreams_in_fedora, #has_desc?, #initialize, #label=, #rdf_type, #set_size_labels, #update_datastream_attributes

Instance Method Details

#assert_content_modelObject



26
27
28
29
# File 'app/models/generic_resource.rb', line 26

def assert_content_model
  super
  add_relationship(:rdf_type, RDF::CUL.Resource.to_s)
end

#index_type_labelObject



35
36
37
# File 'app/models/generic_resource.rb', line 35

def index_type_label
  "FILE ASSET"
end

#linkable_resourcesObject



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
# File 'app/models/generic_resource.rb', line 103

def linkable_resources
  # let's start with the known DSIDs from lindquist, then work our way back to parsing the solrized relsint
  results = []
  if (rels = rels_int.instance_variable_get :@solr_hash)
    # this was loaded from solr
    rels.each do |dsuri, props|
      if dsuri =~ /\/content$/ or not props[FORMAT_OF_PREDICATE].blank?
        dsid =  dsuri.split('/')[-1]
        puts props.inspect
        res = datastream_as_resource(dsid, props.with_indifferent_access)
        results << res
      end
    end
  else
    content_uri = RDF::URI("info:fedora/#{self.pid}/content")
    dsuris = [content_uri]
    results = []
    # read the graph
    datastreams.each do |k, v|
      rels = rels_int.relationships(v, :format_of, content_uri)
      dsuris << rels[0].subject unless rels.blank?
    end

    dsuris.each do |dsuri|
      dsid = dsuri.to_s.split('/')[-1]
      width_rel = rels_int.relationships(dsuri, :image_width)[0]
      length_rel = rels_int.relationships(dsuri, :image_length)[0]
      extent_rel = rels_int.relationships(dsuri, :extent)[0]
      props = {extent: [], image_width: [], image_length: []}
      props[:extent] << extent_rel.object.to_s unless extent_rel.blank?
      props[:image_width] << width_rel.object.to_s unless width_rel.blank?
      props[:image_length] << length_rel.object.to_s unless length_rel.blank?
      results << datastream_as_resource(dsid, props)
    end
  end
  results
end

#route_asObject



31
32
33
# File 'app/models/generic_resource.rb', line 31

def route_as
  "resource"
end

#thumbnail_infoObject



39
40
41
42
43
44
45
46
47
# File 'app/models/generic_resource.rb', line 39

def thumbnail_info
  thumb = relsint.relationships(datastreams['content'],:foaf_thumb).first
  if thumb
    t_dsid = thumb.object.to_s.split('/')[-1]
    return {:url=>"#{ActiveFedora.fedora_config[:url]}/objects/#{pid}/datastreams/#{t_dsid}/content",:mime=>datastreams[t_dsid].mimeType}
  else
    return {:url=>image_url("cul_scv_hydra/crystal/file.png"),:mime=>'image/png'}
  end
end

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



49
50
51
52
53
54
55
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
# File 'app/models/generic_resource.rb', line 49

def to_solr(solr_doc = Hash.new, opts={})
  solr_doc = super

  unless solr_doc["extent_ssim"] || self.datastreams["content"].nil?
    if self.datastreams["content"].dsSize.to_i > 0
      solr_doc["extent_ssim"] = [self.datastreams["content"].dsSize]
    else
      repo = ActiveFedora::Base.connection_for_pid(pid)
      ds_parms = {pid: pid, dsid: "content", method: :head}
      repo.datastream_dissemination(ds_parms) do |res|
        solr_doc["extent_ssim"] = res['Content-Length']
      end
    end
  end

  if self.zooming?
    fz = rels_int.relationships(datastreams['content'], :foaf_zooming).first.object.to_s.split('/')[-1]
    ds = datastreams[fz]
    unless ds.nil?
      rft_id = ds.controlGroup == 'E' ? datastreams[fz].dsLocation : legacy_content_path(ds,'info:fedora/datastreams/')
      solr_doc['rft_id_ss'] = rft_id
    end
  end

  solr_doc["fulltext_tesim"] = []
  unless self.datastreams["fulltext"].nil?
    solr_doc["fulltext_tesim"].concat(solr_doc["title_display_ssm"]) unless solr_doc["title_display_ssm"].nil? or solr_doc["title_display_ssm"].length == 0
    solr_doc["fulltext_tesim"] << self.datastreams["fulltext"].content
  end
  relationships(:original_name).each do |original_name|
    solr_doc["original_name_tesim"] ||= []
    original_name = original_name.object.to_s.split('/').join(' ')
    solr_doc["original_name_tesim"] << original_name.strip
  end

  solr_doc
end

#with_content_resource(fedora_content_filesystem_mounted = false, &block) ⇒ Object



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
# File 'app/models/generic_resource.rb', line 145

def with_content_resource(fedora_content_filesystem_mounted=false, &block)

  content_ds = self.datastreams['content']

  if fedora_content_filesystem_mounted
    if content_ds.dsLocation =~ /^file:\//
      dsLocation = content_ds.dsLocation.sub(/^file:\/+/,'/')
      path = URI.unescape(dsLocation)
    else
      path = content_ds.dsLocation
    end

    yield(path)
  else
    internal_uri = "info:fedora/#{self.pid}/content"
    # No local fedora mount, need to download content over http[s]

    file_basename = File.basename(content_ds.dsLocation.gsub(/^file:/,''))
    file_extension = File.extname(file_basename)

    # In some cases, we actually do want to know the original extension of the file, so we'll preserve it in the temp file filename
    temp_file = Tempfile.new([file_basename, file_extension])
    begin
      parts = internal_uri.split('/')
      open(temp_file.path, 'wb') do |blob|
        repo = ActiveFedora::Base.connection_for_pid(parts[1])
        repo.datastream_dissemination({:dsid=>parts[2], :pid=>parts[1], :finished=>false}) do |res|
          res.read_body do |seg|
            blob << seg
          end
        end
      end
      yield(temp_file.path)
    ensure
      temp_file.unlink
    end
  end

end

#zooming?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/generic_resource.rb', line 141

def zooming?
  (zr = rels_int.relationships(datastreams['content'], :foaf_zooming) and not zr.first.blank?)
end