Class: FileAsset

Inherits:
ActiveFedora::Base
  • Object
show all
Includes:
Hydra::ModelMethods
Defined in:
vendor/plugins/hydra_repository/app/models/file_asset.rb

Direct Known Subclasses

AudioAsset, ImageAsset, VideoAsset

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Hydra::ModelMethods

#apply_depositor_metadata, #insert_contributor, #remove_contributor, #set_collection_type

Class Method Details

+ (Object) garbage_collect(pid)

deletes the object identified by pid if it does not have any objects asserting has_collection_member



9
10
11
12
13
14
15
16
17
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 9

def self.garbage_collect(pid)
  begin 
    obj = FileAsset.load_instance(pid)
    if obj.containers.empty?
      obj.delete
    end
  rescue
  end
end

Instance Method Details

- (Object) add_file_datastream(file, opts = {})

augments add_file_datastream to also put file size (in bytes/KB/MB/GB/TB) in dc:extent



41
42
43
44
45
46
47
48
49
50
51
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 41

def add_file_datastream(file, opts={})
  super
  if file.respond_to?(:size)
    size = bits_to_human_readable(file.size)
  elsif file.kind_of?(File)
    size = bits_to_human_readable(File.size(file))
  else
    size = ""
  end
  datastreams_in_memory["descMetadata"].extent_values = size
end

- (Object) bits_to_human_readable(num)

Returns a human readable filesize appropriate for the given number of bytes (ie. automatically chooses ‘bytes’,’KB’,’MB’,’GB’,’TB’) Based on a bit of python code posted here: blogmag.net/blog/read/38/Print_human_readable_file_size



22
23
24
25
26
27
28
29
30
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 22

def bits_to_human_readable(num)
    ['bytes','KB','MB','GB','TB'].each do |x|
      if num < 1024.0
        return "#{num.to_i} #{x}"
      else
        num = num/1024.0
      end
    end
end

- (Array) containers(opts = {})

Mimic the relationship accessor that would be created if a containers relationship existed Decided to create this method instead because it combines more than one relationship list from is_member_of_collection and part_of

Parameters:

  • (Hash) opts (defaults to: {})

    The options hash that can contain a :response_format value of :id_array, :solr, or :load_from_solr

Returns:

  • (Array)

    Objects found through inbound has_collection_member and part_of relationships



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 58

def containers(opts={})
  is_member_array = is_member_of_collection(:response_format=>:id_array)
    
  if !is_member_array.empty?
    logger.warn "This object has inbound collection member assertions.  hasCollectionMember will no longer be used to track file_object relationships after active_fedora 1.3.  Use isPartOf assertions in the RELS-EXT of child objects instead."
    if opts[:response_format] == :solr || opts[:response_format] == :load_from_solr
      logger.warn ":solr and :load_from_solr response formats for containers search only uses parts relationships (usage of hasCollectionMember is no longer supported)"
      result = part_of(opts)
    else
      con_result = is_member_of_collection(opts)
      part_of_result = part_of(opts)
      ary = con_result+part_of_result
      result = ary.uniq
    end
  else
    result = part_of(opts)
  end
  return result
end

- (Array) containers_from_solr

Calls containers with the option to load objects found from solr instead of Fedora.

Returns:

  • (Array)

    ActiveFedora::Base objects populated via solr



86
87
88
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 86

def containers_from_solr
  containers(:response_format => :load_from_solr)
end

- (Array) containers_ids

Calls containers with the :id_array option to return a list of pids for containers found.

Returns:

  • (Array)

    Container ids (via is_member_of_collection and part_of relationships)



80
81
82
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 80

def containers_ids
  containers(:response_format => :id_array)
end

- (Object) label=(label)



35
36
37
38
# File 'vendor/plugins/hydra_repository/app/models/file_asset.rb', line 35

def label=(label)
  super
  datastreams_in_memory["descMetadata"].title_values = label
end