class Valkyrie::Storage::Shrine

Constants

PROTOCOL

Attributes

identifier_prefix[R]
path_generator[R]
shrine[R]
verifier[R]

Public Class Methods

new(shrine_storage, verifier = nil, path_generator = IDPathGenerator, identifier_prefix: nil) click to toggle source
# File lib/valkyrie/storage/shrine.rb, line 44
def initialize(shrine_storage, verifier = nil, path_generator = IDPathGenerator, identifier_prefix: nil)
  @path_generator = path_generator.new(base_path: "")
  @shrine = shrine_storage
  @identifier_prefix = identifier_prefix
  if verifier.nil?
    try_to_find_verifier
  else
    @verifier = verifier
  end
end

Public Instance Methods

delete(id:) click to toggle source

Delete the file in S3 associated with the given identifier. @param id [Valkyrie::ID]

# File lib/valkyrie/storage/shrine.rb, line 94
def delete(id:)
  shrine.delete(shrine_id_for(id))
end
find_by(id:) click to toggle source

Return the file associated with the given identifier @param id [Valkyrie::ID] @return [Valkyrie::StorageAdapter::StreamFile] @raise Valkyrie::StorageAdapter::FileNotFound if nothing is found

# File lib/valkyrie/storage/shrine.rb, line 79
def find_by(id:)
  raise Valkyrie::StorageAdapter::FileNotFound unless shrine.exists?(shrine_id_for(id))
  Valkyrie::StorageAdapter::StreamFile.new(id: Valkyrie::ID.new(id.to_s), io: DelayedDownload.new(shrine, shrine_id_for(id)))
rescue Aws::S3::Errors::NoSuchKey
  raise Valkyrie::StorageAdapter::FileNotFound
end
handles?(id:) click to toggle source

@param id [Valkyrie::ID] @return [Boolean] true if this adapter can handle this type of identifier

# File lib/valkyrie/storage/shrine.rb, line 88
def handles?(id:)
  id.to_s.start_with?(protocol_with_prefix)
end
upload(file:, original_filename:, resource:, **upload_options) click to toggle source

Upload a file via the Shrine storage upload implementation @param file [IO] @param original_filename [String] @param resource [Valkyrie::Resource] @return [Valkyrie::StorageAdapter::StreamFile] @raise Valkyrie::Shrine::IntegrityError if verify_checksum is defined

on the shrine object and the file and result digests do not match
# File lib/valkyrie/storage/shrine.rb, line 62
def upload(file:, original_filename:, resource:, **upload_options)
  # S3 adapter validates options, so we have to remove this one used in
  # the shared specs.
  upload_options.delete(:fake_upload_argument)
  identifier = path_generator.generate(resource: resource, file: file, original_filename: original_filename).to_s
  shrine.upload(file, identifier, **upload_options)
  find_by(id: "#{protocol_with_prefix}#{identifier}").tap do |result|
    if verifier
      raise Valkyrie::Shrine::IntegrityError unless verifier.verify_checksum(file, result)
    end
  end
end

Private Instance Methods

protocol_with_prefix() click to toggle source
# File lib/valkyrie/storage/shrine.rb, line 109
def protocol_with_prefix
  [identifier_prefix, PROTOCOL].compact.join("-")
end
shrine_id_for(id) click to toggle source
# File lib/valkyrie/storage/shrine.rb, line 105
def shrine_id_for(id)
  id.to_s.sub(/^#{protocol_with_prefix}/, '')
end
try_to_find_verifier() click to toggle source
# File lib/valkyrie/storage/shrine.rb, line 100
def try_to_find_verifier
  class_const = shrine.class.name.split(/::/).last.to_sym
  @verifier = Valkyrie::Shrine::Checksum.const_get(class_const).new if Valkyrie::Shrine::Checksum.const_defined?(class_const)
end