demarches-normaliennes/lib/active_storage/service/cellar_service.rb

81 lines
2.3 KiB
Ruby
Raw Normal View History

module ActiveStorage
class Service::CellarService < Service
def initialize(access_key_id:, secret_access_key:, bucket:, **)
@adapter = Cellar::CellarAdapter.new(access_key_id, secret_access_key, bucket)
end
2018-02-20 12:09:05 +01:00
def upload(key, io, checksum: nil)
instrument :upload, key: key, checksum: checksum do
@adapter.session { |s| s.upload(key, io, checksum) }
2018-02-20 12:09:05 +01:00
end
end
def download(key, &block)
2018-02-14 16:38:22 +01:00
if block_given?
instrument :streaming_download, key: key do
@adapter.session { |s| s.download(key, &block) }
2018-02-14 16:38:22 +01:00
end
else
instrument :download, key: key do
@adapter.session { |s| s.download(key) }
2018-02-14 15:40:41 +01:00
end
end
end
2018-02-13 18:47:26 +01:00
def delete(key)
instrument :delete, key: key do
@adapter.session { |s| s.delete(key) }
2018-02-14 18:58:22 +01:00
end
end
def delete_prefixed(prefix)
instrument :delete_prefixed, prefix: prefix do
@adapter.session do |s|
keys = s.list_prefixed(prefix)
s.delete_keys(keys)
2018-02-13 18:47:26 +01:00
end
end
end
2018-02-16 12:13:45 +01:00
def exist?(key)
instrument :exist, key: key do |payload|
answer = @adapter.session { |s| s.exist?(key) }
payload[:exist] = answer
answer
2018-02-16 12:13:45 +01:00
end
end
def url(key, expires_in:, filename:, disposition:, content_type:)
instrument :url, key: key do |payload|
generated_url = @adapter.presigned_url(
method: 'GET',
key: key,
expires_in: expires_in,
"response-content-disposition": content_disposition_with(type: disposition, filename: filename),
"response-content-type": content_type
)
payload[:url] = generated_url
generated_url
end
end
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
generated_url = @adapter.presigned_url(
method: 'PUT',
key: key,
expires_in: expires_in,
content_type: content_type,
checksum: checksum
)
payload[:url] = generated_url
generated_url
end
end
def headers_for_direct_upload(key, content_type:, checksum:, **)
{ "Content-Type" => content_type, "Content-MD5" => checksum }
end
end
end