Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/active_storage/service/bunny_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ module ActiveStorage
# See ActiveStorage::Service for the generic API documentation that applies to all services.
class Service::BunnyService < Service

attr_reader :client, :base_url
attr_reader :client, :base_url, :access_key, :storage_zone, :region

def initialize(access_key:, api_key:, storage_zone:, region:, cdn: false)
@client = BunnyStorageClient.new(access_key, api_key, storage_zone, region)
@access_key = access_key
@storage_zone = storage_zone
@region = region

if cdn
@base_url = cdn
Expand Down Expand Up @@ -41,6 +44,14 @@ def download(key, &block)
end
end

def download_chunk(key, range)
instrument :download_chunk, key: key, range: range do
io = StringIO.new object_for(key).get_file(range: "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}")

io.set_encoding(Encoding::BINARY)
end
end

def delete(key)
instrument :delete, key: key do
object_for(key).delete_file
Expand Down Expand Up @@ -73,20 +84,16 @@ def url(key, expires_in:, disposition:, filename:, **options)

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
instrument :url, key: key do |payload|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
content_type: content_type, content_length: content_length, content_md5: checksum,
metadata: custom_metadata, whitelist_headers: ['content-length'], **upload_options

upload_base_url = region ? "#{region}.storage.bunnycdn.com" : "storage.bunnycdn.com"
generated_url = "https://#{upload_base_url}/#{storage_zone}/#{key}"
payload[:url] = generated_url

generated_url
end
end

def headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}, **)
content_disposition = content_disposition_with(type: disposition, filename: filename) if filename

{ 'Content-Type' => content_type, 'Content-MD5' => checksum, 'Content-Disposition' => content_disposition, **custom_metadata_headers(custom_metadata) }
{ 'AccessKey' => access_key, 'Content-Type' => 'application/octet-stream' }
end

private
Expand All @@ -100,6 +107,10 @@ def public_url(key)
File.join(base_url, key)
end

def custom_metadata_headers(custom_metadata)
{}
end

def upload_with_single_part(key, io, checksum: nil, content_type: nil, content_disposition: nil, custom_metadata: {})
object_for(key).upload_file(body: io)
object_for(key).purge_cache
Expand Down
2 changes: 1 addition & 1 deletion lib/active_storage_bunny/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveStorageBunny
VERSION = '1.0.1'.freeze
VERSION = '1.0.3'.freeze
end