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 28, 2022
1 parent 497c052 commit dfaaef5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/valkyrie/storage/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ def upload(file:, original_filename:, resource: nil, **_extra_arguments)
new_path = path_generator.generate(resource: resource, file: file, original_filename: original_filename)
FileUtils.mkdir_p(new_path.parent)
file_mover.call(file.path, new_path)
find_by(id: Valkyrie::ID.new("disk://#{new_path}"))

identifier = id_for(file: file, original_filename: original_filename, resource: resource)
find_by(id: identifier)
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]
Expand Down
17 changes: 17 additions & 0 deletions lib/valkyrie/storage/fedora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def find_by(id:)
# @return [Valkyrie::StorageAdapter::StreamFile]
def upload(file:, original_filename:, resource:, content_type: "application/octet-stream", # rubocop:disable Metrics/ParameterLists
resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments)

# we can't use id_for here because this id requires furhter transformation below
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
Expand All @@ -48,9 +51,23 @@ def upload(file:, original_filename:, resource:, content_type: "application/octe
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

# @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
11 changes: 10 additions & 1 deletion lib/valkyrie/storage/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ def initialize
# @param _extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::StorageAdapter::StreamFile]
def upload(file:, original_filename:, resource: nil, **_extra_arguments)
identifier = Valkyrie::ID.new("memory://#{resource.id}")
identifier = id_for(file: file, original_filename: original_filename, resource: resource)
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 dfaaef5

Please sign in to comment.