Class: ActiveFedora::File

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveTriples::Properties, Deprecation
Includes:
AttributeMethods, Persistence, Streaming, Versionable, ActiveModel::Dirty
Defined in:
lib/active_fedora/file.rb

Overview

This class represents a Fedora datastream

Direct Known Subclasses

Datastream, NomDatastream, OmDatastream, RDFDatastream

Defined Under Namespace

Modules: Persistence, Streaming

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Versionable

#create_version, #has_versions?, #model_type, #restore_version, #versions

Methods included from Streaming

#stream

Methods included from Persistence

#content, #content=, #retrieve_content, #save

Methods included from AttributeMethods

#[], #[]=, #attribute_names, #attributes

Constructor Details

#initialize(parent_or_url_or_hash = nil, path = nil, options = {}) ⇒ File

Returns a new instance of File.

Parameters:

  • parent_or_url (ActiveFedora::Base, String, Hash, NilClass)

    the parent resource or the URI of this resource

  • path_name (String)

    the path partial relative to the resource

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


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_fedora/file.rb', line 18

def initialize(parent_or_url_or_hash = nil, path=nil, options={})
  case parent_or_url_or_hash
  when Hash
    content = ''
    @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, nil, content, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
  when nil, String
  #TODO this is similar to Core#build_ldp_resource
    content = ''
    @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, parent_or_url_or_hash, content, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
  when ActiveFedora::Base
    Deprecation.warn File, "Initializing a file by passing a container is deprecated. Initialize with a uri instead. This capability will be removed in active-fedora 10.0"
    uri = if parent_or_url_or_hash.uri.kind_of?(::RDF::URI) && parent_or_url_or_hash.uri.value.empty?
      nil
    else
      "#{parent_or_url_or_hash.uri}/#{path}"
    end
    @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri, nil, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)

  else
    raise "The first argument to #{self} must be a String or an ActiveFedora::Base. You provided a #{parent_or_url.class}"
  end

  @attributes = {}.with_indifferent_access
end

Instance Attribute Details

#mime_typeObject



105
106
107
108
# File 'lib/active_fedora/file.rb', line 105

def mime_type
  @mime_type ||= fetch_mime_type unless new_record?
  @mime_type || default_mime_type
end

Class Method Details

.default_attributesObject



154
155
156
# File 'lib/active_fedora/file.rb', line 154

def default_attributes
  {}
end

Instance Method Details

#attribute_will_change!(attr) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/active_fedora/file.rb', line 91

def attribute_will_change!(attr)
  if attr == 'content'
    changed_attributes['content'] = true
  else
    super
  end
end

#changed?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/active_fedora/file.rb', line 148

def changed?
  super || content_changed?
end

#check_fixityObject



83
84
85
# File 'lib/active_fedora/file.rb', line 83

def check_fixity
  FixityService.new(@ldp_source.subject).check
end

#content_changed?Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'lib/active_fedora/file.rb', line 143

def content_changed?
  return true if new_record? and !local_or_remote_content(false).blank?
  local_or_remote_content(false) != @ds_content
end

#datastream_will_change!Object



87
88
89
# File 'lib/active_fedora/file.rb', line 87

def datastream_will_change!
  attribute_will_change! :profile
end

#default_attributesObject



159
160
161
# File 'lib/active_fedora/file.rb', line 159

def default_attributes
  @default_attributes ||= self.class.default_attributes
end

#default_attributes=(attributes) ⇒ Object



163
164
165
# File 'lib/active_fedora/file.rb', line 163

def default_attributes= attributes
  @default_attributes = default_attributes.merge attributes
end

#described_byObject



47
48
49
50
# File 'lib/active_fedora/file.rb', line 47

def described_by
  raise "#{self} isn't persisted yet" if new_record?
  links['describedby'].first
end

#digestObject



118
119
120
121
# File 'lib/active_fedora/file.rb', line 118

def digest
  response = .ldp_source.graph.query(predicate: ActiveFedora::RDF::Fcrepo4.digest)
  response.map(&:object)
end

#dirty_sizeObject



127
128
129
# File 'lib/active_fedora/file.rb', line 127

def dirty_size
  content.size if changed? && content.respond_to?(:size)
end

#empty?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_fedora/file.rb', line 139

def empty?
  !has_content?
end

#freezeObject

Freeze datastreams such that they can be loaded from Fedora, but can’t be changed



182
183
184
# File 'lib/active_fedora/file.rb', line 182

def freeze
  @frozen = true
end

#frozen?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/active_fedora/file.rb', line 186

def frozen?
  !!@frozen
end

#has_content?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/active_fedora/file.rb', line 135

def has_content?
  size && size > 0
end

#inspectObject



171
172
173
# File 'lib/active_fedora/file.rb', line 171

def inspect
  "#<#{self.class} uri=\"#{uri}\" >"
end

#ldp_connectionObject



52
53
54
# File 'lib/active_fedora/file.rb', line 52

def ldp_connection
  ActiveFedora.fedora.connection
end

#ldp_sourceObject



43
44
45
# File 'lib/active_fedora/file.rb', line 43

def ldp_source
  @ldp_source || raise("NO source")
end

#metadataObject



110
111
112
# File 'lib/active_fedora/file.rb', line 110

def 
  @metadata ||= ActiveFedora::WithMetadata::MetadataNode.new(self)
end

#metadata?boolean

This method is abstract.

Override this in your concrete datastream class.

Returns does this datastream contain metadata (not file data).

Returns:

  • (boolean)

    does this datastream contain metadata (not file data)



177
178
179
# File 'lib/active_fedora/file.rb', line 177

def metadata?
  false
end

#new_record?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_fedora/file.rb', line 61

def new_record?
  ldp_source.new?
end

#original_nameObject



114
115
116
# File 'lib/active_fedora/file.rb', line 114

def original_name
  @original_name ||= fetch_original_name_from_headers
end

#original_name=(name) ⇒ Object



167
168
169
# File 'lib/active_fedora/file.rb', line 167

def original_name= name
  @original_name = name
end

#persisted_sizeObject



123
124
125
# File 'lib/active_fedora/file.rb', line 123

def persisted_size
  ldp_source.head.headers['Content-Length'].to_i unless new_record?
end

#reloadObject

When restoring from previous versions, we need to reload certain attributes from Fedora



70
71
72
73
# File 'lib/active_fedora/file.rb', line 70

def reload
  return if new_record?
  reset
end

#remote_contentObject



99
100
101
102
# File 'lib/active_fedora/file.rb', line 99

def remote_content
  return if new_record?
  @ds_content ||= retrieve_content
end

#resetObject



75
76
77
78
79
80
81
# File 'lib/active_fedora/file.rb', line 75

def reset
  @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri)
  @original_name = nil
  @mime_type = nil
  @content = nil
  @metadata = nil
end

#serialize!Object

serializes any changed data into the content field



191
192
# File 'lib/active_fedora/file.rb', line 191

def serialize!
end

#sizeObject



131
132
133
# File 'lib/active_fedora/file.rb', line 131

def size
  dirty_size || persisted_size
end

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



194
195
196
# File 'lib/active_fedora/file.rb', line 194

def to_solr(solr_doc={}, opts={})
  solr_doc
end

#uriObject

TODO this is like FedoraAttributes#uri



57
58
59
# File 'lib/active_fedora/file.rb', line 57

def uri
  ldp_source.subject
end

#uri=(uri) ⇒ Object



65
66
67
# File 'lib/active_fedora/file.rb', line 65

def uri= uri
  @ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri, '', ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
end