better_storage 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/better_storage/attachment.rb +13 -4
- data/lib/better_storage/blob.rb +12 -5
- data/lib/better_storage/railtie.rb +9 -1
- data/lib/better_storage/s3_service_proxy.rb +6 -6
- data/lib/better_storage/variant.rb +11 -0
- data/lib/better_storage/version.rb +1 -1
- data/lib/better_storage.rb +23 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 000e62192f58bc6619a6a900e46aeaab4e5877b9d76457e5ba1efec8a90c5c41
|
4
|
+
data.tar.gz: 0ab04353ca0bcc20e1fb8362862c5ad6b16c814db73abfe01693ba08d37bb56b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08dbfb6408d2ca3859ea751ca3267c7719f2459abe885f2ced3ff38e824fb3148171c9a1d3a4dbfe29ccf953f8ec7fcd6d16ce6b1975a68417d88d42509a77c6'
|
7
|
+
data.tar.gz: 2f9beb8a09522bf7e7234a1177ecefbc02d381b9587069232c3efeeb74e7009c88994fe5d4556843e84639d73971ebc5c303bd380e7356abd555d910ac511236
|
data/README.md
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
# better_storage.rb initializer
|
11
11
|
|
12
12
|
BetterStorage.configure do |config|
|
13
|
+
config.namespace = "storage" # prefix for ActiveStorage uploads default: nil
|
13
14
|
config.protect_production_files = true # default: Rails.env.development?
|
14
15
|
config.prefix_date_format = "%Y/%m/%d" # default: "%Y%m"
|
15
16
|
end
|
@@ -34,6 +35,12 @@ Add this line to your application's Gemfile:
|
|
34
35
|
gem "better_storage"
|
35
36
|
```
|
36
37
|
|
38
|
+
## Warning
|
39
|
+
|
40
|
+
`ActiveStorage.track_variants = true` is assumed. This gem is not tested with `ActiveStorage.track_variants = false`.
|
41
|
+
With the false setting, `variant_class` is ActiveStorage::Variant instead of ActiveStorage::VariantWithRecord.
|
42
|
+
ActiveStorage::Variant implements its own special blob key.
|
43
|
+
|
37
44
|
## Contributing
|
38
45
|
Contribution directions go here.
|
39
46
|
|
@@ -6,11 +6,20 @@ module BetterStorage
|
|
6
6
|
key = if style == :original
|
7
7
|
self.key
|
8
8
|
else
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
transformations = if Rails.version.to_f >= 7.1
|
10
|
+
variants = record.attachment_reflections[name]&.named_variants
|
11
|
+
variants.fetch(style) do
|
12
|
+
record_model_name = record.to_model.model_name.name
|
13
|
+
raise ArgumentError, "Cannot find variant :#{style} for #{record_model_name}##{name}"
|
14
|
+
end.transformations
|
15
|
+
else
|
16
|
+
variants = record.attachment_reflections[name]&.variants
|
17
|
+
variants.fetch(style) do
|
18
|
+
record_model_name = record.to_model.model_name.name
|
19
|
+
raise ArgumentError, "Cannot find variant :#{style} for #{record_model_name}##{name}"
|
20
|
+
end
|
13
21
|
end
|
22
|
+
|
14
23
|
transformation_key = ActiveStorage::Variation.wrap(transformations).digest
|
15
24
|
variant_cache_key = "#{id}-#{transformation_key}"
|
16
25
|
|
data/lib/better_storage/blob.rb
CHANGED
@@ -2,16 +2,23 @@ module BetterStorage
|
|
2
2
|
module Blob
|
3
3
|
def key
|
4
4
|
self[:key] ||= begin
|
5
|
-
|
6
|
-
|
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
|
@@ -3,7 +3,9 @@ module BetterStorage
|
|
3
3
|
|
4
4
|
initializer "better_storage.configs" do
|
5
5
|
config.after_initialize do |app|
|
6
|
-
BetterStorage.s3_endpoint ||= ActiveStorage::Blob.service.bucket
|
6
|
+
BetterStorage.s3_endpoint ||= if ActiveStorage::Blob.service.respond_to?(:bucket)
|
7
|
+
ActiveStorage::Blob.service.bucket.url
|
8
|
+
end
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
@@ -13,6 +15,12 @@ module BetterStorage
|
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
18
|
+
initializer "better_storage.variant" do
|
19
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
20
|
+
ActiveStorage::Variant.prepend BetterStorage::Variant
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
16
24
|
initializer "better_storage.attachment" do
|
17
25
|
ActiveSupport.on_load(:active_storage_blob) do
|
18
26
|
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
|
5
|
+
return false if should_protect?(key)
|
10
6
|
super
|
11
7
|
end
|
12
8
|
|
13
9
|
def delete_prefixed(prefix)
|
14
|
-
return
|
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
|
data/lib/better_storage.rb
CHANGED
@@ -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
|
4
|
+
version: 0.2.1
|
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-
|
11
|
+
date: 2023-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: appraisal
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: enhance active_storage
|
84
98
|
email:
|
85
99
|
- yfxie@me.com
|
@@ -95,6 +109,7 @@ files:
|
|
95
109
|
- lib/better_storage/blob.rb
|
96
110
|
- lib/better_storage/railtie.rb
|
97
111
|
- lib/better_storage/s3_service_proxy.rb
|
112
|
+
- lib/better_storage/variant.rb
|
98
113
|
- lib/better_storage/version.rb
|
99
114
|
homepage: https://github.com/yfxie/better_storage/
|
100
115
|
licenses:
|
@@ -115,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
130
|
- !ruby/object:Gem::Version
|
116
131
|
version: '0'
|
117
132
|
requirements: []
|
118
|
-
rubygems_version: 3.4.
|
133
|
+
rubygems_version: 3.4.22
|
119
134
|
signing_key:
|
120
135
|
specification_version: 4
|
121
136
|
summary: enhance active_storage
|