Skip to content

Commit

Permalink
add StorageAdapter#id_for
Browse files Browse the repository at this point in the history
Closes GH-887.
  • Loading branch information
dunn committed Oct 20, 2022
1 parent 497c052 commit 4ab4982
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/valkyrie/storage/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def upload(file:, original_filename:, resource: nil, **_extra_arguments)
find_by(id: Valkyrie::ID.new("disk://#{new_path}"))
end

# @param file [IO]
# @param original_filename [String]
# @param resource [Valkyrie::Resource]
# @param _extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::ID]
def id_for(file:, original_filename:, resource: nil, **_extra_arguments)
new_path = path_generator.generate(resource: resource, file: file, original_filename: original_filename)
Valkyrie::ID.new("disk://#{new_path}")
end

# @param id [Valkyrie::ID]
# @return [Boolean] true if this adapter can handle this type of identifer
def handles?(id:)
Expand Down
13 changes: 13 additions & 0 deletions lib/valkyrie/storage/fedora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def upload(file:, original_filename:, resource:, content_type: "application/octe
find_by(id: Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL)))
end

# @param file [IO]
# @param original_filename [String]
# @param resource [Valkyrie::Resource]
# @param content_type [String] content type of file (e.g. 'image/tiff') (default='application/octet-stream')
# @param resource_uri_transformer [Lambda] transforms the resource's id (e.g. 'DDS78RK') into a uri (optional)
# @param extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::StorageAdapter::StreamFile]
def id_for(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'
Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL))
end

# Delete the file in Fedora associated with the given identifier.
# @param id [Valkyrie::ID]
def delete(id:)
Expand Down
9 changes: 9 additions & 0 deletions lib/valkyrie/storage/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def upload(file:, original_filename:, resource: nil, **_extra_arguments)
cache[identifier] = Valkyrie::StorageAdapter::StreamFile.new(id: identifier, io: file)
end

# @param file [IO]
# @param original_filename [String]
# @param resource [Valkyrie::Resource]
# @param _extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::StorageAdapter::StreamFile]
def id_for(file:, original_filename:, resource: nil, **_extra_arguments)
Valkyrie::ID.new("memory://#{resource.id}")
end

# Return the file associated with the given identifier
# @param id [Valkyrie::ID]
# @return [Valkyrie::StorageAdapter::StreamFile]
Expand Down

0 comments on commit 4ab4982

Please sign in to comment.