From 4ab4982af6a7ea8f2b93fa28997180a07aef0d47 Mon Sep 17 00:00:00 2001 From: Alexandra Dunn Date: Thu, 20 Oct 2022 18:11:30 +0000 Subject: [PATCH] add StorageAdapter#id_for Closes GH-887. --- lib/valkyrie/storage/disk.rb | 10 ++++++++++ lib/valkyrie/storage/fedora.rb | 13 +++++++++++++ lib/valkyrie/storage/memory.rb | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/lib/valkyrie/storage/disk.rb b/lib/valkyrie/storage/disk.rb index 87975ea76..2461260be 100644 --- a/lib/valkyrie/storage/disk.rb +++ b/lib/valkyrie/storage/disk.rb @@ -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:) diff --git a/lib/valkyrie/storage/fedora.rb b/lib/valkyrie/storage/fedora.rb index 6957942cb..8c56e1570 100644 --- a/lib/valkyrie/storage/fedora.rb +++ b/lib/valkyrie/storage/fedora.rb @@ -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:) diff --git a/lib/valkyrie/storage/memory.rb b/lib/valkyrie/storage/memory.rb index 6f102b16f..ee5c79281 100644 --- a/lib/valkyrie/storage/memory.rb +++ b/lib/valkyrie/storage/memory.rb @@ -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]