better_storage 0.1.0 → 0.2.0

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: 9939f2580116c2a947922d368527cf204fa74aebbc24a517ca1f307202818e13
4
- data.tar.gz: 85894eb542fd5e20ddb88debbd0f94e8687992748a16f2238bf70290436dcdf2
3
+ metadata.gz: c648e03cd0078c3c9db4ca303108a6e7d94ead8cf9209c8254a142ab6e76bfe0
4
+ data.tar.gz: 8ed57e8a1d12ba42c0d5131783a1dafeb421fc1f22d2b1a85003e80df004aa85
5
5
  SHA512:
6
- metadata.gz: 1379973176a5f74b84cbf81dac587dbbb7767ad1e693c578146320b2bc58763d91e96e7876f41f8cf350b5766978255cda8109608466b91f5f282b9f378dcd57
7
- data.tar.gz: 7cdb282c50c3f9f5bf1629f3ea9e91396a4d587fc39cbfa4b62c6ff3b88c506f7fcf53df6f1459da75dd254704abe221fd90b2ef66be44434327c99a86b43a74
6
+ metadata.gz: 7e823c08ad67aa4abf3f88d989c10839fa88b511f31fded4793d61af4acc4c37f16a601ceca9b59d284d242785f0c6e866aa56c48d04e81f768133e87eadc137
7
+ data.tar.gz: 49e72c46273de6ebd737674cf284477968ba548701edf5a42e55768119c461a7c49eaba2588331d23cbe0a3ad57d71abfb440174625b58f263248876f3b82186
data/README.md CHANGED
@@ -34,6 +34,12 @@ Add this line to your application's Gemfile:
34
34
  gem "better_storage"
35
35
  ```
36
36
 
37
+ ## Warning
38
+
39
+ `ActiveStorage.track_variants = true` is assumed. This gem is not tested with `ActiveStorage.track_variants = false`.
40
+ With the false setting, `variant_class` is ActiveStorage::Variant instead of ActiveStorage::VariantWithRecord.
41
+ ActiveStorage::Variant implements its own special blob key.
42
+
37
43
  ## Contributing
38
44
  Contribution directions go here.
39
45
 
@@ -2,16 +2,23 @@ module BetterStorage
2
2
  module Blob
3
3
  def key
4
4
  self[:key] ||= begin
5
- parts = []
6
- parts << "dev" if Rails.env.development?
7
- parts << Date.today.strftime(BetterStorage.prefix_date_format) if BetterStorage.prefix_date_format
8
- parts << self.class.generate_unique_secure_token(length: self.class::MINIMUM_TOKEN_LENGTH)
9
- parts.join("/")
5
+ key = self.class.generate_unique_secure_token(length: self.class::MINIMUM_TOKEN_LENGTH)
6
+ BetterStorage.generate_blob_key(key)
10
7
  end
11
8
  end
12
9
 
13
10
  def public_url
14
11
  BetterStorage.public_url(key)
15
12
  end
13
+
14
+ def delete
15
+ service.delete(key)
16
+ # original implementation is `service.delete_prefixed("variants/#{key}/") if image?`
17
+ service.delete_prefixed(variant_prefix) if image?
18
+ end
19
+
20
+ def variant_prefix
21
+ "#{key}/variants/"
22
+ end
16
23
  end
17
24
  end
@@ -13,6 +13,12 @@ module BetterStorage
13
13
  end
14
14
  end
15
15
 
16
+ initializer "better_storage.variant" do
17
+ ActiveSupport.on_load(:active_storage_blob) do
18
+ ActiveStorage::Variant.prepend BetterStorage::Variant
19
+ end
20
+ end
21
+
16
22
  initializer "better_storage.attachment" do
17
23
  ActiveSupport.on_load(:active_storage_blob) do
18
24
  ActiveStorage::Attachment.include BetterStorage::Attachment
@@ -1,18 +1,18 @@
1
1
  module BetterStorage
2
2
  # protect production files from deletion in development
3
3
  module S3ServiceProxy
4
- def is_protected?(key)
5
- BetterStorage.protect_production_files && !key.start_with?("dev/")
6
- end
7
-
8
4
  def delete(key)
9
- return true if is_protected?(key)
5
+ return false if should_protect?(key)
10
6
  super
11
7
  end
12
8
 
13
9
  def delete_prefixed(prefix)
14
- return true if is_protected?(prefix)
10
+ return false if should_protect?(prefix)
15
11
  super
16
12
  end
13
+
14
+ def should_protect?(key)
15
+ BetterStorage.protect_production_files && BetterStorage.protected_key?(key)
16
+ end
17
17
  end
18
18
  end
@@ -0,0 +1,11 @@
1
+ module BetterStorage
2
+ module Variant
3
+ # original implementation:
4
+ # def key
5
+ # "variants/#{blob.key}/#{OpenSSL::Digest::SHA256.hexdigest(variation.key)}"
6
+ # end
7
+ def key
8
+ "#{blob.key}/variants/#{OpenSSL::Digest::SHA256.hexdigest(variation.key)}"
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module BetterStorage
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,8 +7,10 @@ module BetterStorage
7
7
  autoload :Blob
8
8
  autoload :S3ServiceProxy
9
9
  autoload :Attachment
10
+ autoload :Variant
10
11
 
11
12
  mattr_accessor :s3_endpoint
13
+ mattr_accessor :namespace
12
14
  mattr_accessor :protect_production_files, default: Rails.env.development?
13
15
  mattr_accessor :prefix_date_format, default: "%Y%m"
14
16
 
@@ -16,10 +18,31 @@ module BetterStorage
16
18
  yield self
17
19
  end
18
20
 
21
+ def namespace=(value)
22
+ @namespace = value.gsub(/\/$/, "")
23
+ end
24
+
19
25
  def self.public_url(key)
20
26
  url = URI.parse(s3_endpoint)
21
27
  url.path += '/' unless url.path[-1] == '/'
22
28
  url.path += key
23
29
  url.to_s
24
30
  end
31
+
32
+ def self.protected_key?(key)
33
+ dev_prefix = [namespace, "dev"].compact.join("/")
34
+ !key.start_with?(dev_prefix)
35
+ end
36
+
37
+ def self.prefix
38
+ parts = []
39
+ parts << namespace if namespace
40
+ parts << "dev" if Rails.env.development?
41
+ parts << Date.today.strftime(prefix_date_format) if prefix_date_format
42
+ parts.join("/")
43
+ end
44
+
45
+ def self.generate_blob_key(*key)
46
+ [BetterStorage.prefix, key].compact.join("/")
47
+ end
25
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yi Feng Xie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-15 00:00:00.000000000 Z
11
+ date: 2023-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -95,6 +95,7 @@ files:
95
95
  - lib/better_storage/blob.rb
96
96
  - lib/better_storage/railtie.rb
97
97
  - lib/better_storage/s3_service_proxy.rb
98
+ - lib/better_storage/variant.rb
98
99
  - lib/better_storage/version.rb
99
100
  homepage: https://github.com/yfxie/better_storage/
100
101
  licenses: