Class: Service::BunnyService
- Inherits:
-
Service
- Object
- Service
- Service::BunnyService
- Defined in:
- lib/active_storage/service/bunny_service.rb
Overview
Wraps the BunnyCDN Storage as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key, &block) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}) ⇒ Object
-
#initialize(access_key:, api_key:, storage_zone:, region:, cdn: false) ⇒ BunnyService
constructor
A new instance of BunnyService.
- #upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
- #url(key, expires_in:, disposition:, filename:, **options) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
Constructor Details
#initialize(access_key:, api_key:, storage_zone:, region:, cdn: false) ⇒ BunnyService
Returns a new instance of BunnyService.
13 14 15 16 17 18 19 20 21 |
# File 'lib/active_storage/service/bunny_service.rb', line 13 def initialize(access_key:, api_key:, storage_zone:, region:, cdn: false) @client = BunnyStorageClient.new(access_key, api_key, storage_zone, region) if cdn @base_url = cdn else @base_url = "https://#{storage_zone}.b-cdn.net" end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/active_storage/service/bunny_service.rb', line 11 def base_url @base_url end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/active_storage/service/bunny_service.rb', line 11 def client @client end |
Instance Method Details
#delete(key) ⇒ Object
44 45 46 47 48 |
# File 'lib/active_storage/service/bunny_service.rb', line 44 def delete(key) instrument :delete, key: key do object_for(key).delete_file end end |
#delete_prefixed(prefix) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/active_storage/service/bunny_service.rb', line 50 def delete_prefixed(prefix) delete prefix # instrument :delete_prefixed, prefix: prefix do # # BunnyStorageClient does not natively support this operation yet. # end end |
#download(key, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_storage/service/bunny_service.rb', line 30 def download(key, &block) if block_given? instrument :streaming_download, { key: key } do stream(key, &block) end else instrument :download, key: key do io = StringIO.new object_for(key).get_file io.set_encoding(Encoding::BINARY) end end end |
#exist?(key) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/active_storage/service/bunny_service.rb', line 57 def exist?(key) instrument :exist, key: key do |payload| answer = object_for(key).exist? payload[:exist] = answer answer end end |
#headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disposition: nil, custom_metadata: {}) ⇒ Object
86 87 88 89 90 |
# File 'lib/active_storage/service/bunny_service.rb', line 86 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, **() } end |
#upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/active_storage/service/bunny_service.rb', line 23 def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}, **) instrument :upload, key: key, checksum: checksum do content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename upload_with_single_part key, io, checksum: checksum, content_type: content_type, content_disposition: content_disposition, custom_metadata: end end |
#url(key, expires_in:, disposition:, filename:, **options) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/active_storage/service/bunny_service.rb', line 65 def url(key, expires_in:, disposition:, filename:, **) instrument :url, {key: key} do |payload| url = public_url key payload[:url] = url url end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/active_storage/service/bunny_service.rb', line 74 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: , whitelist_headers: ['content-length'], ** payload[:url] = generated_url generated_url end end |