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

#authorization_key, #headers, #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


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


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

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

Class Method Details

.default_attributesObject


161
162
163
# File 'lib/active_fedora/file.rb', line 161

def default_attributes
  {}
end

Instance Method Details

#attribute_will_change!(attr) ⇒ Object


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

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

#changed?Boolean


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

def changed?
  super || content_changed?
end

#check_fixityObject


90
91
92
# File 'lib/active_fedora/file.rb', line 90

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

#content_changed?Boolean


150
151
152
153
# File 'lib/active_fedora/file.rb', line 150

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


94
95
96
# File 'lib/active_fedora/file.rb', line 94

def datastream_will_change!
  attribute_will_change! :profile
end

#default_attributesObject


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

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

#default_attributes=(attributes) ⇒ Object


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

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


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

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

#dirty_sizeObject


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

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

#empty?Boolean


146
147
148
# File 'lib/active_fedora/file.rb', line 146

def empty?
  !has_content?
end

#exists!Object

If we know the record to exist (parent has LDP:contains), we can avoid unnecessary HEAD requests


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

def exists!
  @exists = true
end

#freezeObject

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


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

def freeze
  @frozen = true
end

#frozen?Boolean


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

def frozen?
  !!@frozen
end

#has_content?Boolean


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

def has_content?
  size && size > 0
end

#inspectObject


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

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


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

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)


184
185
186
# File 'lib/active_fedora/file.rb', line 184

def metadata?
  false
end

#new_record?Boolean

If this file have a parent with ldp#contains, we know it is not new. By tracking exists we prevent an unnecessary HEAD request.


63
64
65
# File 'lib/active_fedora/file.rb', line 63

def new_record?
  !@exists && ldp_source.new?
end

#original_nameObject


121
122
123
# File 'lib/active_fedora/file.rb', line 121

def original_name
  @original_name ||= fetch_original_name_from_headers
end

#original_name=(name) ⇒ Object


174
175
176
# File 'lib/active_fedora/file.rb', line 174

def original_name= name
  @original_name = name
end

#persisted_sizeObject


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

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


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

def reload
  return if new_record?
  reset
end

#remote_contentObject


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

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

#resetObject


82
83
84
85
86
87
88
# File 'lib/active_fedora/file.rb', line 82

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


198
199
# File 'lib/active_fedora/file.rb', line 198

def serialize!
end

#sizeObject


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

def size
  dirty_size || persisted_size
end

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


201
202
203
# File 'lib/active_fedora/file.rb', line 201

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


67
68
69
# File 'lib/active_fedora/file.rb', line 67

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