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://'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:, base_path: "/") ⇒ Fedora

Returns a new instance of Fedora.

Parameters:

  • connection (Ldp::Client)


9
10
11
12
# File 'lib/valkyrie/storage/fedora.rb', line 9

def initialize(connection:, base_path: "/")
  @connection = connection
  @base_path = base_path
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

Instance Method Details

#delete(id:) ⇒ Object

Delete the file in Fedora associated with the given identifier.

Parameters:



46
47
48
# File 'lib/valkyrie/storage/fedora.rb', line 46

def delete(id:)
  connection.http.delete(fedora_identifier(id: id))
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



24
25
26
# File 'lib/valkyrie/storage/fedora.rb', line 24

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



16
17
18
# File 'lib/valkyrie/storage/fedora.rb', line 16

def handles?(id:)
  id.to_s.start_with?(PROTOCOL)
end

#upload(file:, original_filename:, resource:) ⇒ Valkyrie::StorageAdapter::StreamFile

Parameters:

Returns:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/valkyrie/storage/fedora.rb', line 32

def upload(file:, original_filename:, resource:)
  identifier = id_to_uri(resource.id) + '/original'
  connection.http.put do |request|
    request.url identifier
    request.headers['Content-Type'] = file.content_type
    request.headers['Content-Disposition'] = "attachment; filename=\"#{original_filename}\""
    request.headers['digest'] = "sha1=#{Digest::SHA1.file(file)}"
    request.body = file.tempfile.read
  end
  find_by(id: Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL)))
end