Class: ActiveFedora::File
Overview
This class represents a Fedora datastream
Defined Under Namespace
Modules: Persistence, Streaming
Constant Summary
AttributeMethods::AttrNames
Instance Attribute Summary collapse
Class Method 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.
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
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
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_attributes ⇒ Object
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_fixity ⇒ Object
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_attributes ⇒ Object
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_by ⇒ Object
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
|
#digest ⇒ Object
125
126
127
128
|
# File 'lib/active_fedora/file.rb', line 125
def digest
response = metadata.ldp_source.graph.query(predicate: ActiveFedora::RDF::Fcrepo4.digest)
response.map(&:object)
end
|
#dirty_size ⇒ Object
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
|
#freeze ⇒ Object
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
|
#inspect ⇒ Object
178
179
180
|
# File 'lib/active_fedora/file.rb', line 178
def inspect
"#<#{self.class} uri=\"#{uri}\" >"
end
|
#ldp_connection ⇒ Object
52
53
54
|
# File 'lib/active_fedora/file.rb', line 52
def ldp_connection
ActiveFedora.fedora.connection
end
|
#ldp_source ⇒ Object
43
44
45
|
# File 'lib/active_fedora/file.rb', line 43
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).
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_name ⇒ Object
121
122
123
|
# File 'lib/active_fedora/file.rb', line 121
def original_name
@original_name ||=
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_size ⇒ Object
130
131
132
|
# File 'lib/active_fedora/file.rb', line 130
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
77
78
79
80
|
# File 'lib/active_fedora/file.rb', line 77
def reload
return if new_record?
reset
end
|
#remote_content ⇒ Object
106
107
108
109
|
# File 'lib/active_fedora/file.rb', line 106
def remote_content
return if new_record?
@ds_content ||= retrieve_content
end
|
#reset ⇒ Object
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
|
#size ⇒ Object
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
|
#uri ⇒ Object
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
|