Class: Valkyrie::Storage::Fedora
- Inherits:
-
Object
- Object
- Valkyrie::Storage::Fedora
- Defined in:
- lib/valkyrie/storage/fedora.rb
Overview
Implements the DataMapper Pattern to store binary data in fedora
Constant Summary collapse
- PROTOCOL =
'fedora://'
- SLASH =
'/'
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#fedora_version ⇒ Object
readonly
Returns the value of attribute fedora_version.
Instance Method Summary collapse
-
#delete(id:) ⇒ Object
Delete the file in Fedora associated with the given identifier.
-
#fedora_identifier(id:) ⇒ RDF::URI
Translate the Valkrie ID into a URL for the fedora file.
-
#find_by(id:) ⇒ Valkyrie::StorageAdapter::StreamFile
Return the file associated with the given identifier.
-
#handles?(id:) ⇒ Boolean
True if this adapter can handle this type of identifer.
-
#initialize(connection:, base_path: "/", fedora_version: Valkyrie::Persistence::Fedora::DEFAULT_FEDORA_VERSION) ⇒ Fedora
constructor
A new instance of Fedora.
- #upload(file:, original_filename:, resource:, content_type: "application/octet-stream", resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::StreamFile
Constructor Details
#initialize(connection:, base_path: "/", fedora_version: Valkyrie::Persistence::Fedora::DEFAULT_FEDORA_VERSION) ⇒ Fedora
Returns a new instance of Fedora.
10 11 12 13 14 |
# File 'lib/valkyrie/storage/fedora.rb', line 10 def initialize(connection:, base_path: "/", fedora_version: Valkyrie::Persistence::Fedora::DEFAULT_FEDORA_VERSION) @connection = connection @base_path = base_path @fedora_version = fedora_version end |
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
5 6 7 |
# File 'lib/valkyrie/storage/fedora.rb', line 5 def base_path @base_path end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
5 6 7 |
# File 'lib/valkyrie/storage/fedora.rb', line 5 def connection @connection end |
#fedora_version ⇒ Object (readonly)
Returns the value of attribute fedora_version.
5 6 7 |
# File 'lib/valkyrie/storage/fedora.rb', line 5 def fedora_version @fedora_version end |
Instance Method Details
#delete(id:) ⇒ Object
Delete the file in Fedora associated with the given identifier.
56 57 58 |
# File 'lib/valkyrie/storage/fedora.rb', line 56 def delete(id:) connection.http.delete(fedora_identifier(id: id)) end |
#fedora_identifier(id:) ⇒ RDF::URI
Translate the Valkrie ID into a URL for the fedora file
79 80 81 82 |
# File 'lib/valkyrie/storage/fedora.rb', line 79 def fedora_identifier(id:) identifier = id.to_s.sub(PROTOCOL, "#{connection.http.scheme}://") RDF::URI(identifier) end |
#find_by(id:) ⇒ Valkyrie::StorageAdapter::StreamFile
Return the file associated with the given identifier
26 27 28 |
# File 'lib/valkyrie/storage/fedora.rb', line 26 def find_by(id:) Valkyrie::StorageAdapter::StreamFile.new(id: id, io: response(id: id)) end |
#handles?(id:) ⇒ Boolean
Returns true if this adapter can handle this type of identifer.
18 19 20 |
# File 'lib/valkyrie/storage/fedora.rb', line 18 def handles?(id:) id.to_s.start_with?(PROTOCOL) end |
#upload(file:, original_filename:, resource:, content_type: "application/octet-stream", resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::StreamFile
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/valkyrie/storage/fedora.rb', line 37 def upload(file:, original_filename:, resource:, content_type: "application/octet-stream", # rubocop:disable Metrics/ParameterLists resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments) identifier = resource_uri_transformer.call(resource, base_url) + '/original' sha1 = [5, 6].include?(fedora_version) ? "sha" : "sha1" connection.http.put do |request| request.url identifier request.headers['Content-Type'] = content_type request.headers['Content-Length'] = file.length.to_s request.headers['Content-Disposition'] = "attachment; filename=\"#{original_filename}\"" request.headers['digest'] = "#{sha1}=#{Digest::SHA1.file(file)}" request.headers['link'] = "<http://www.w3.org/ns/ldp#NonRDFSource>; rel=\"type\"" io = Faraday::UploadIO.new(file, content_type, original_filename) request.body = io end find_by(id: Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL))) end |