better_storage 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +41 -0
- data/Rakefile +11 -0
- data/lib/better_storage/attachment.rb +24 -0
- data/lib/better_storage/blob.rb +17 -0
- data/lib/better_storage/railtie.rb +29 -0
- data/lib/better_storage/s3_service_proxy.rb +18 -0
- data/lib/better_storage/version.rb +3 -0
- data/lib/better_storage.rb +25 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9939f2580116c2a947922d368527cf204fa74aebbc24a517ca1f307202818e13
|
4
|
+
data.tar.gz: 85894eb542fd5e20ddb88debbd0f94e8687992748a16f2238bf70290436dcdf2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1379973176a5f74b84cbf81dac587dbbb7767ad1e693c578146320b2bc58763d91e96e7876f41f8cf350b5766978255cda8109608466b91f5f282b9f378dcd57
|
7
|
+
data.tar.gz: 7cdb282c50c3f9f5bf1629f3ea9e91396a4d587fc39cbfa4b62c6ff3b88c506f7fcf53df6f1459da75dd254704abe221fd90b2ef66be44434327c99a86b43a74
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Yi Feng Xie
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# BetterStorage
|
2
|
+
|
3
|
+
* Prefix ActiveStorage uploads with `dev` and customized datetime format.
|
4
|
+
* Protect production files from deletion in development environment.
|
5
|
+
* `public_url` method for ActiveStorage attachments and blobs.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# better_storage.rb initializer
|
11
|
+
|
12
|
+
BetterStorage.configure do |config|
|
13
|
+
config.protect_production_files = true # default: Rails.env.development?
|
14
|
+
config.prefix_date_format = "%Y/%m/%d" # default: "%Y%m"
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ApplicationRecord
|
20
|
+
has_one_attached :avatar do |attachable|
|
21
|
+
attachable.variant :thumb, resize_to_limit: [120, 120]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
user = User.first
|
26
|
+
user.avatar.public_url
|
27
|
+
user.avatar.public_url(:thumb)
|
28
|
+
```
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
gem "better_storage"
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
Contribution directions go here.
|
39
|
+
|
40
|
+
## License
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module BetterStorage
|
2
|
+
module Attachment
|
3
|
+
def public_url(style = :original)
|
4
|
+
return nil unless persisted?
|
5
|
+
|
6
|
+
key = if style == :original
|
7
|
+
self.key
|
8
|
+
else
|
9
|
+
variants = record.attachment_reflections[name]&.variants
|
10
|
+
transformations = variants.fetch(style) do
|
11
|
+
record_model_name = record.to_model.model_name.name
|
12
|
+
raise ArgumentError, "Cannot find variant :#{style} for #{record_model_name}##{name}"
|
13
|
+
end
|
14
|
+
transformation_key = ActiveStorage::Variation.wrap(transformations).digest
|
15
|
+
variant_cache_key = "#{id}-#{transformation_key}"
|
16
|
+
|
17
|
+
Rails.cache.fetch([:better_storage_public_url, variant_cache_key, BetterStorage::VERSION]) do
|
18
|
+
variant(style).processed.key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
BetterStorage.public_url(key)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module BetterStorage
|
2
|
+
module Blob
|
3
|
+
def key
|
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("/")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def public_url
|
14
|
+
BetterStorage.public_url(key)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module BetterStorage
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
|
4
|
+
initializer "better_storage.configs" do
|
5
|
+
config.after_initialize do |app|
|
6
|
+
BetterStorage.s3_endpoint ||= ActiveStorage::Blob.service.bucket.url
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer "better_storage.blob" do
|
11
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
12
|
+
prepend BetterStorage::Blob
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer "better_storage.attachment" do
|
17
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
18
|
+
ActiveStorage::Attachment.include BetterStorage::Attachment
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
initializer "better_storage.service" do
|
23
|
+
config.after_initialize do
|
24
|
+
require "active_storage/service/s3_service"
|
25
|
+
ActiveStorage::Service::S3Service.prepend BetterStorage::S3ServiceProxy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module BetterStorage
|
2
|
+
# protect production files from deletion in development
|
3
|
+
module S3ServiceProxy
|
4
|
+
def is_protected?(key)
|
5
|
+
BetterStorage.protect_production_files && !key.start_with?("dev/")
|
6
|
+
end
|
7
|
+
|
8
|
+
def delete(key)
|
9
|
+
return true if is_protected?(key)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete_prefixed(prefix)
|
14
|
+
return true if is_protected?(prefix)
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "better_storage/version"
|
2
|
+
require "better_storage/railtie"
|
3
|
+
|
4
|
+
module BetterStorage
|
5
|
+
extend ActiveSupport::Autoload
|
6
|
+
|
7
|
+
autoload :Blob
|
8
|
+
autoload :S3ServiceProxy
|
9
|
+
autoload :Attachment
|
10
|
+
|
11
|
+
mattr_accessor :s3_endpoint
|
12
|
+
mattr_accessor :protect_production_files, default: Rails.env.development?
|
13
|
+
mattr_accessor :prefix_date_format, default: "%Y%m"
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield self
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.public_url(key)
|
20
|
+
url = URI.parse(s3_endpoint)
|
21
|
+
url.path += '/' unless url.path[-1] == '/'
|
22
|
+
url.path += key
|
23
|
+
url.to_s
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yi Feng Xie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 7.0.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: aws-sdk-s3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: enhance active_storage
|
84
|
+
email:
|
85
|
+
- yfxie@me.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- lib/better_storage.rb
|
94
|
+
- lib/better_storage/attachment.rb
|
95
|
+
- lib/better_storage/blob.rb
|
96
|
+
- lib/better_storage/railtie.rb
|
97
|
+
- lib/better_storage/s3_service_proxy.rb
|
98
|
+
- lib/better_storage/version.rb
|
99
|
+
homepage: https://github.com/yfxie/better_storage/
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubygems_version: 3.4.10
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: enhance active_storage
|
122
|
+
test_files: []
|