Class: ActiveFedora::File
Overview
This class represents a Fedora datastream
Defined Under Namespace
Modules: Persistence, Streaming
Constant Summary
AttributeMethods::AttrNames
Instance Attribute Summary collapse
Instance Method Summary
collapse
#create_version, #has_versions?, #model_type, #restore_version, #versions
Methods included from Streaming
#authorization_key, #headers, #stream
#content, #content=, #retrieve_content, #save
#[], #[]=, #attribute_names, #attributes
Constructor Details
#initialize(parent_or_url_or_hash = nil, path = nil, options = {}) ⇒ File
Returns a new instance of File.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/active_fedora/file.rb', line 20
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
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_type ⇒ Object
114
115
116
117
|
# File 'lib/active_fedora/file.rb', line 114
def mime_type
@mime_type ||= fetch_mime_type unless new_record?
@mime_type || default_mime_type
end
|
Instance Method Details
#attribute_will_change!(attr) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/active_fedora/file.rb', line 100
def attribute_will_change!(attr)
if attr == 'content'
changed_attributes['content'] = true
else
super
end
end
|
#changed? ⇒ Boolean
157
158
159
|
# File 'lib/active_fedora/file.rb', line 157
def changed?
super || content_changed?
end
|
#check_fixity ⇒ Object
92
93
94
|
# File 'lib/active_fedora/file.rb', line 92
def check_fixity
FixityService.new(@ldp_source.subject).check
end
|
#content_changed? ⇒ Boolean
152
153
154
155
|
# File 'lib/active_fedora/file.rb', line 152
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
96
97
98
|
# File 'lib/active_fedora/file.rb', line 96
def datastream_will_change!
attribute_will_change! :profile
end
|
#described_by ⇒ Object
49
50
51
52
|
# File 'lib/active_fedora/file.rb', line 49
def described_by
raise "#{self} isn't persisted yet" if new_record?
links['describedby'].first
end
|
#digest ⇒ Object
127
128
129
130
|
# File 'lib/active_fedora/file.rb', line 127
def digest
response = metadata.ldp_source.graph.query(predicate: ActiveFedora::RDF::Fcrepo4.digest)
response.map(&:object)
end
|
#dirty_size ⇒ Object
136
137
138
|
# File 'lib/active_fedora/file.rb', line 136
def dirty_size
content.size if changed? && content.respond_to?(:size)
end
|
#empty? ⇒ Boolean
148
149
150
|
# File 'lib/active_fedora/file.rb', line 148
def empty?
!has_content?
end
|
#exists! ⇒ Object
If we know the record to exist (parent has LDP:contains), we can avoid unnecessary HEAD requests
74
75
76
|
# File 'lib/active_fedora/file.rb', line 74
def exists!
@exists = true
end
|
#freeze ⇒ Object
Freeze datastreams such that they can be loaded from Fedora, but can’t be changed
176
177
178
|
# File 'lib/active_fedora/file.rb', line 176
def freeze
@frozen = true
end
|
#frozen? ⇒ Boolean
180
181
182
|
# File 'lib/active_fedora/file.rb', line 180
def frozen?
!!@frozen
end
|
#has_content? ⇒ Boolean
144
145
146
|
# File 'lib/active_fedora/file.rb', line 144
def has_content?
size && size > 0
end
|
#inspect ⇒ Object
165
166
167
|
# File 'lib/active_fedora/file.rb', line 165
def inspect
"#<#{self.class} uri=\"#{uri}\" >"
end
|
#ldp_connection ⇒ Object
54
55
56
|
# File 'lib/active_fedora/file.rb', line 54
def ldp_connection
ActiveFedora.fedora.connection
end
|
#ldp_source ⇒ Object
45
46
47
|
# File 'lib/active_fedora/file.rb', line 45
def ldp_source
@ldp_source || raise("NO source")
end
|
This method is abstract.
Override this in your concrete datastream class.
Returns does this datastream contain metadata (not file data).
171
172
173
|
# File 'lib/active_fedora/file.rb', line 171
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.
65
66
67
|
# File 'lib/active_fedora/file.rb', line 65
def new_record?
!@exists && ldp_source.new?
end
|
#original_name ⇒ Object
123
124
125
|
# File 'lib/active_fedora/file.rb', line 123
def original_name
@original_name ||=
end
|
#original_name=(name) ⇒ Object
161
162
163
|
# File 'lib/active_fedora/file.rb', line 161
def original_name= name
@original_name = name
end
|
#persisted_size ⇒ Object
132
133
134
|
# File 'lib/active_fedora/file.rb', line 132
def persisted_size
ldp_source.head.['Content-Length'].to_i unless new_record?
end
|
#reload ⇒ Object
When restoring from previous versions, we need to reload certain attributes from Fedora
79
80
81
82
|
# File 'lib/active_fedora/file.rb', line 79
def reload
return if new_record?
reset
end
|
#remote_content ⇒ Object
108
109
110
111
|
# File 'lib/active_fedora/file.rb', line 108
def remote_content
return if new_record?
@ds_content ||= retrieve_content
end
|
#reset ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/active_fedora/file.rb', line 84
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
185
186
|
# File 'lib/active_fedora/file.rb', line 185
def serialize!
end
|
#size ⇒ Object
140
141
142
|
# File 'lib/active_fedora/file.rb', line 140
def size
dirty_size || persisted_size
end
|
#to_solr(solr_doc = {}, opts = {}) ⇒ Object
188
189
190
|
# File 'lib/active_fedora/file.rb', line 188
def to_solr(solr_doc={}, opts={})
solr_doc
end
|
#uri ⇒ Object
TODO this is like FedoraAttributes#uri
59
60
61
|
# File 'lib/active_fedora/file.rb', line 59
def uri
ldp_source.subject
end
|
#uri=(uri) ⇒ Object
69
70
71
|
# File 'lib/active_fedora/file.rb', line 69
def uri= uri
@ldp_source = Ldp::Resource::BinarySource.new(ldp_connection, uri, '', ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
end
|