2018-02-07 20:53:51 +01:00
|
|
|
module ActiveStorage
|
|
|
|
class Service::CellarService < Service
|
|
|
|
def initialize(access_key_id:, secret_access_key:, bucket:, **)
|
2018-02-22 15:18:58 +01:00
|
|
|
@adapter = Cellar::CellarAdapter.new(access_key_id, secret_access_key, bucket)
|
2018-02-07 20:53:51 +01:00
|
|
|
end
|
|
|
|
|
2018-02-20 12:09:05 +01:00
|
|
|
def upload(key, io, checksum: nil)
|
|
|
|
instrument :upload, key: key, checksum: checksum do
|
2018-02-22 15:18:58 +01:00
|
|
|
@adapter.session { |s| s.upload(key, io, checksum) }
|
2018-02-20 12:09:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-22 15:18:58 +01:00
|
|
|
def download(key, &block)
|
2018-02-14 16:38:22 +01:00
|
|
|
if block_given?
|
|
|
|
instrument :streaming_download, key: key do
|
2018-02-22 15:18:58 +01:00
|
|
|
@adapter.session { |s| s.download(key, &block) }
|
2018-02-14 16:38:22 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
instrument :download, key: key do
|
2018-02-22 15:18:58 +01:00
|
|
|
@adapter.session { |s| s.download(key) }
|
2018-02-14 15:40:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-24 14:01:32 +02:00
|
|
|
def download_chunk(key, range)
|
|
|
|
instrument :download_chunk, key: key, range: range do
|
|
|
|
@adapter.session { |s| s.download(key, range: range) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-13 18:47:26 +01:00
|
|
|
def delete(key)
|
|
|
|
instrument :delete, key: key do
|
2018-02-22 15:18:58 +01:00
|
|
|
@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
|
2018-02-22 15:18:58 +01:00
|
|
|
@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|
|
2018-02-22 15:18:58 +01:00
|
|
|
answer = @adapter.session { |s| s.exist?(key) }
|
|
|
|
payload[:exist] = answer
|
|
|
|
answer
|
2018-02-16 12:13:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-07 20:53:51 +01:00
|
|
|
def url(key, expires_in:, filename:, disposition:, content_type:)
|
|
|
|
instrument :url, key: key do |payload|
|
2018-02-22 15:18:58 +01:00
|
|
|
generated_url = @adapter.presigned_url(
|
2018-02-07 20:53:51 +01:00
|
|
|
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|
|
2018-02-22 15:18:58 +01:00
|
|
|
generated_url = @adapter.presigned_url(
|
2018-02-07 20:53:51 +01:00
|
|
|
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
|