diff --git a/lib/active_storage/service/bunny_service.rb b/lib/active_storage/service/bunny_service.rb index ac69daf..72e6192 100644 --- a/lib/active_storage/service/bunny_service.rb +++ b/lib/active_storage/service/bunny_service.rb @@ -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 @@ -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 @@ -73,10 +84,8 @@ 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 @@ -84,9 +93,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec 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 @@ -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 diff --git a/lib/active_storage_bunny/version.rb b/lib/active_storage_bunny/version.rb index e3f93f1..045f733 100644 --- a/lib/active_storage_bunny/version.rb +++ b/lib/active_storage_bunny/version.rb @@ -1,3 +1,3 @@ module ActiveStorageBunny - VERSION = '1.0.1'.freeze + VERSION = '1.0.3'.freeze end