active_storage_bunny 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74d8ca0414380a8f138a9eb2f3f5d7cb4c4f9aac29c6e2aae5dace40532bd689
4
- data.tar.gz: 45e0cb4b9be82b4bb3228792481dd5a8734bcde61e3941841d88bcadb6e808fb
3
+ metadata.gz: a3a4228b63965eea285975f7afabeeac6e9bb2f3bde3f24638c2d6e662ace50b
4
+ data.tar.gz: eee8cd70a77ef6b5eb1d6cdc12f294d45f54f051ca3b344cb48148cf8e03fac0
5
5
  SHA512:
6
- metadata.gz: 1362ce11eb5dc5022c037547724b6f230fc5b9fab0857ba258ff1a5dbf52a6a11625d12df5dfe2956326b7d77340737bffd969cfb55f5902be5b981929fd1133
7
- data.tar.gz: 007041caf3ab0f9b3e42787df1be4344acb2ee045631295fe8a9b3c8129e6d45b61d80d4f910528bf0befce53e16db4232ccc2056722febde6dd1992a553c800
6
+ metadata.gz: 46100469320d9f53dfff19edcee376fdede6fbffe42a6662df4913133fea5ebd232af76b670c92910f402fc474addecc27a24dcca3e6ce347f0501ce1a7eb9eb
7
+ data.tar.gz: 5b38882608dfc9636ea0ad89dd5345ca1cc6f9c0b06ee581e146cf295e0a4041ea8d1d30f0f4411b845fba782ce3e239c06a037bb45dc7430f3cf3362ef79dd0
@@ -4,17 +4,20 @@ require 'active_support/core_ext/numeric/bytes'
4
4
  require 'bunny_storage_client'
5
5
 
6
6
  module ActiveStorage
7
- # Wraps the Amazon Simple Storage Service (S3) as an Active Storage service.
7
+ # Wraps the BunnyCDN Storage as an Active Storage service.
8
8
  # See ActiveStorage::Service for the generic API documentation that applies to all services.
9
9
  class Service::BunnyService < Service
10
10
 
11
- CDN_BASE_URL = "https://#{ENV['BUNNY_STORAGE_ZONE']}.b-cdn.net"
11
+ attr_reader :client, :base_url
12
12
 
13
- attr_reader :client
13
+ def initialize(access_key:, api_key:, storage_zone:, region:, cdn: false)
14
+ @client = BunnyStorageClient.new(access_key, api_key, storage_zone, region)
14
15
 
15
- def initialize(access_key:, api_key:, storage_zone:, **options)
16
- @client = BunnyStorageClient.new(access_key, api_key, storage_zone)
17
- super(**options)
16
+ if cdn
17
+ @base_url = cdn
18
+ else
19
+ @base_url = "https://#{storage_zone}.b-cdn.net"
20
+ end
18
21
  end
19
22
 
20
23
  def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}, **)
@@ -25,10 +28,16 @@ module ActiveStorage
25
28
  end
26
29
 
27
30
  def download(key, &block)
28
- instrument :download, key: key do
29
- object_for(key).get_file.to_s.force_encoding(Encoding::BINARY)
30
- rescue StandardError
31
- raise ActiveStorage::FileNotFoundError
31
+ if block_given?
32
+ instrument :streaming_download, { key: key } do
33
+ stream(key, &block)
34
+ end
35
+ else
36
+ instrument :download, key: key do
37
+ io = StringIO.new object_for(key).get_file
38
+
39
+ io.set_encoding(Encoding::BINARY)
40
+ end
32
41
  end
33
42
  end
34
43
 
@@ -39,20 +48,29 @@ module ActiveStorage
39
48
  end
40
49
 
41
50
  def delete_prefixed(prefix)
42
- instrument :delete_prefixed, prefix: prefix do
43
- # BunnyStorageClient does not natively support this operation yet.
44
- end
51
+ delete prefix
52
+ # instrument :delete_prefixed, prefix: prefix do
53
+ # # BunnyStorageClient does not natively support this operation yet.
54
+ # end
45
55
  end
46
56
 
47
-
48
57
  def exist?(key)
49
58
  instrument :exist, key: key do |payload|
50
- answer = object_for(key).exists?
59
+ answer = object_for(key).exist?
51
60
  payload[:exist] = answer
52
61
  answer
53
62
  end
54
63
  end
55
64
 
65
+ def url(key, expires_in:, disposition:, filename:, **options)
66
+ instrument :url, {key: key} do |payload|
67
+ url = public_url key
68
+ payload[:url] = url
69
+
70
+ url
71
+ end
72
+ end
73
+
56
74
  def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
57
75
  instrument :url, key: key do |payload|
58
76
  generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
@@ -79,7 +97,7 @@ module ActiveStorage
79
97
  end
80
98
 
81
99
  def public_url(key)
82
- File.join(CDN_BASE_URL, key)
100
+ File.join(base_url, key)
83
101
  end
84
102
 
85
103
  def upload_with_single_part(key, io, checksum: nil, content_type: nil, content_disposition: nil, custom_metadata: {})
@@ -89,8 +107,15 @@ module ActiveStorage
89
107
  raise ActiveStorage::IntegrityError
90
108
  end
91
109
 
92
- def stream_file(key)
93
- # BunnyStorageClient does not natively support this operation yet.
110
+ def stream(key, options = {}, &block)
111
+ io = StringIO.new object_for(key).get_file
112
+ io.set_encoding(Encoding::BINARY)
113
+
114
+ chunk_size = 5.megabytes
115
+
116
+ while chunk = io.read(chunk_size)
117
+ yield chunk
118
+ end
94
119
  end
95
120
 
96
121
  def object_for(key)
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageBunny
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramit Koul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-20 00:00:00.000000000 Z
11
+ date: 2024-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny_storage_client
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '6.0'
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
- version: '8.0'
36
+ version: '9.0'
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '6.0'
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: '8.0'
46
+ version: '9.0'
47
47
  description: ActiveStorageBunny is a gem that integrates BunnyCDN storage services
48
48
  with Active Storage.
49
49
  email: