Class: Valkyrie::Storage::Fedora

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(connection:, base_path: "/", fedora_version: Valkyrie::Persistence::Fedora::DEFAULT_FEDORA_VERSION) ⇒ Fedora

Returns a new instance of Fedora.

Parameters:

  • connection (Ldp::Client)

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_pathObject (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

#connectionObject (readonly)

Returns the value of attribute connection


5
6
7
# File 'lib/valkyrie/storage/fedora.rb', line 5

def connection
  @connection
end

#fedora_versionObject (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.

Parameters:


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

Returns:


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

Parameters:

Returns:

Raises:

  • Valkyrie::StorageAdapter::FileNotFound if nothing is found


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.

Parameters:

Returns:

  • (Boolean)

    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

Parameters:

  • file (IO)
  • original_filename (String)
  • resource (Valkyrie::Resource)
  • content_type (String) (defaults to: "application/octet-stream")

    content type of file (e.g. 'image/tiff') (default='application/octet-stream')

  • resource_uri_transformer (Lambda) (defaults to: default_resource_uri_transformer)

    transforms the resource's id (e.g. 'DDS78RK') into a uri (optional)

  • extra_arguments (Hash)

    additional arguments which may be passed to other adapters

Returns:


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