activestorage_saas 5.2.5.1
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/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +112 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +12 -0
- data/activestorage_saas.gemspec +33 -0
- data/app/controller/active_storage_saas/base_controller.rb +7 -0
- data/app/controller/active_storage_saas/blobs_controller.rb +8 -0
- data/app/controller/active_storage_saas/direct_uploads_controller.rb +26 -0
- data/app/controller/active_storage_saas/disk_controller.rb +63 -0
- data/app/controller/active_storage_saas/representations_controller.rb +14 -0
- data/db/migrate/001_activestorage_saas_tables.rb +17 -0
- data/lib/active_storage/service/saas_service.rb +71 -0
- data/lib/active_storage_saas.rb +11 -0
- data/lib/actives_torage_saas/blob_patch.rb +41 -0
- data/lib/actives_torage_saas/engine.rb +18 -0
- data/lib/actives_torage_saas/routes.rb +41 -0
- data/lib/activestorage_saas.rb +1 -0
- data/sig/activestorage_saas.rbs +4 -0
- metadata +83 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d1b5d632c8d8737928c64de3799d5871205fdc50b94306574a85323d47f22eda
|
|
4
|
+
data.tar.gz: 8c8b86b0f37605aa95ea80ddf6cb88c44f5ab74c70e81a2e684d25aa3c692754
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 819a58f6f078ca03aba8736559ac5e04379e54076f80264ad363b1f9007913033797aaff0896b2bbf418e246f13f91092232d13edb836471c25a634736001a33
|
|
7
|
+
data.tar.gz: 9bf4a83504cc82ffe698f6f3725eb7c82b642a6d018ba3d14d1269b1f8d20408ea45c93ec3338e326280e491d125e8e739b856fc6c95fb4a513c828d788e719d
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.6
|
|
3
|
+
NewCops: enable
|
|
4
|
+
Style/StringLiterals:
|
|
5
|
+
Enabled: true
|
|
6
|
+
EnforcedStyle: single_quotes
|
|
7
|
+
|
|
8
|
+
Style/StringLiteralsInInterpolation:
|
|
9
|
+
Enabled: true
|
|
10
|
+
EnforcedStyle: double_quotes
|
|
11
|
+
|
|
12
|
+
Layout/LineLength:
|
|
13
|
+
Max: 120
|
|
14
|
+
|
|
15
|
+
Layout/IndentationConsistency:
|
|
16
|
+
Enabled: true
|
|
17
|
+
EnforcedStyle: indented_internal_methods
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
activestorage_saas (5.2.5.1)
|
|
5
|
+
activestorage (= 5.2.5)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
actionpack (5.2.5)
|
|
11
|
+
actionview (= 5.2.5)
|
|
12
|
+
activesupport (= 5.2.5)
|
|
13
|
+
rack (~> 2.0, >= 2.0.8)
|
|
14
|
+
rack-test (>= 0.6.3)
|
|
15
|
+
rails-dom-testing (~> 2.0)
|
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
17
|
+
actionview (5.2.5)
|
|
18
|
+
activesupport (= 5.2.5)
|
|
19
|
+
builder (~> 3.1)
|
|
20
|
+
erubi (~> 1.4)
|
|
21
|
+
rails-dom-testing (~> 2.0)
|
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
23
|
+
activemodel (5.2.5)
|
|
24
|
+
activesupport (= 5.2.5)
|
|
25
|
+
activerecord (5.2.5)
|
|
26
|
+
activemodel (= 5.2.5)
|
|
27
|
+
activesupport (= 5.2.5)
|
|
28
|
+
arel (>= 9.0)
|
|
29
|
+
activestorage (5.2.5)
|
|
30
|
+
actionpack (= 5.2.5)
|
|
31
|
+
activerecord (= 5.2.5)
|
|
32
|
+
marcel (~> 1.0.0)
|
|
33
|
+
activesupport (5.2.5)
|
|
34
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
35
|
+
i18n (>= 0.7, < 2)
|
|
36
|
+
minitest (~> 5.1)
|
|
37
|
+
tzinfo (~> 1.1)
|
|
38
|
+
arel (9.0.0)
|
|
39
|
+
ast (2.4.2)
|
|
40
|
+
builder (3.2.4)
|
|
41
|
+
concurrent-ruby (1.1.10)
|
|
42
|
+
crass (1.0.6)
|
|
43
|
+
diff-lcs (1.5.0)
|
|
44
|
+
erubi (1.11.0)
|
|
45
|
+
i18n (1.12.0)
|
|
46
|
+
concurrent-ruby (~> 1.0)
|
|
47
|
+
loofah (2.18.0)
|
|
48
|
+
crass (~> 1.0.2)
|
|
49
|
+
nokogiri (>= 1.5.9)
|
|
50
|
+
marcel (1.0.2)
|
|
51
|
+
mini_portile2 (2.8.0)
|
|
52
|
+
minitest (5.16.2)
|
|
53
|
+
nokogiri (1.13.8)
|
|
54
|
+
mini_portile2 (~> 2.8.0)
|
|
55
|
+
racc (~> 1.4)
|
|
56
|
+
parallel (1.22.1)
|
|
57
|
+
parser (3.1.1.0)
|
|
58
|
+
ast (~> 2.4.1)
|
|
59
|
+
racc (1.6.0)
|
|
60
|
+
rack (2.2.4)
|
|
61
|
+
rack-test (2.0.2)
|
|
62
|
+
rack (>= 1.3)
|
|
63
|
+
rails-dom-testing (2.0.3)
|
|
64
|
+
activesupport (>= 4.2.0)
|
|
65
|
+
nokogiri (>= 1.6)
|
|
66
|
+
rails-html-sanitizer (1.4.3)
|
|
67
|
+
loofah (~> 2.3)
|
|
68
|
+
rainbow (3.1.1)
|
|
69
|
+
rake (13.0.6)
|
|
70
|
+
regexp_parser (2.5.0)
|
|
71
|
+
rexml (3.2.5)
|
|
72
|
+
rspec (3.11.0)
|
|
73
|
+
rspec-core (~> 3.11.0)
|
|
74
|
+
rspec-expectations (~> 3.11.0)
|
|
75
|
+
rspec-mocks (~> 3.11.0)
|
|
76
|
+
rspec-core (3.11.0)
|
|
77
|
+
rspec-support (~> 3.11.0)
|
|
78
|
+
rspec-expectations (3.11.0)
|
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
80
|
+
rspec-support (~> 3.11.0)
|
|
81
|
+
rspec-mocks (3.11.1)
|
|
82
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
83
|
+
rspec-support (~> 3.11.0)
|
|
84
|
+
rspec-support (3.11.0)
|
|
85
|
+
rubocop (1.27.0)
|
|
86
|
+
parallel (~> 1.10)
|
|
87
|
+
parser (>= 3.1.0.0)
|
|
88
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
89
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
90
|
+
rexml
|
|
91
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
|
92
|
+
ruby-progressbar (~> 1.7)
|
|
93
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
94
|
+
rubocop-ast (1.17.0)
|
|
95
|
+
parser (>= 3.1.1.0)
|
|
96
|
+
ruby-progressbar (1.11.0)
|
|
97
|
+
thread_safe (0.3.6)
|
|
98
|
+
tzinfo (1.2.10)
|
|
99
|
+
thread_safe (~> 0.1)
|
|
100
|
+
unicode-display_width (2.1.0)
|
|
101
|
+
|
|
102
|
+
PLATFORMS
|
|
103
|
+
ruby
|
|
104
|
+
|
|
105
|
+
DEPENDENCIES
|
|
106
|
+
activestorage_saas!
|
|
107
|
+
rake (~> 13.0)
|
|
108
|
+
rspec (~> 3.0)
|
|
109
|
+
rubocop (~> 1.21)
|
|
110
|
+
|
|
111
|
+
BUNDLED WITH
|
|
112
|
+
2.3.10
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 张小辉
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# ActivestorageSaas
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activestorage_saas`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
10
|
+
|
|
11
|
+
$ bundle add activestorage_saas
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
$ gem install activestorage_saas
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
TODO: Write usage instructions here
|
|
20
|
+
|
|
21
|
+
## Development
|
|
22
|
+
|
|
23
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
24
|
+
|
|
25
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activestorage_saas.
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
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,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
activestorage_version = '5.2.5'
|
|
4
|
+
gem_version = 1
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "activestorage_saas"
|
|
8
|
+
spec.version = "#{activestorage_version}.#{gem_version}"
|
|
9
|
+
spec.authors = ["xiaohui"]
|
|
10
|
+
spec.email = ["xiaohui@tanmer.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "Wraps multi-tenant storage services as ActiveStorage service"
|
|
13
|
+
spec.description = "Each tenant can set its own storage service, ActiveStorage service can be dynamically loaded."
|
|
14
|
+
spec.homepage = "https://github.com/xiaohui-zhangxh/activestorage_saas"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
spec.required_ruby_version = ">= 2.6.0"
|
|
17
|
+
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
|
|
21
|
+
|
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
spec.require_paths = ["lib"]
|
|
31
|
+
|
|
32
|
+
spec.add_dependency "activestorage", activestorage_version
|
|
33
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class ActiveStorageSaas::DirectUploadsController < ActiveStorageSaas::BaseController
|
|
2
|
+
def create
|
|
3
|
+
blob = ActiveStorage::Blob.create!(blob_args)
|
|
4
|
+
render json: direct_upload_json(blob)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def callback
|
|
8
|
+
raise NotImplementedError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def blob_args
|
|
14
|
+
blob_args = params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type).to_h.symbolize_keys
|
|
15
|
+
blob_args.merge! tenant: current_tenant, tenant_storage_service: current_tenant.storage_service
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def direct_upload_json(blob)
|
|
19
|
+
blob.as_json(root: false, methods: :signed_id, only: :signed_id)
|
|
20
|
+
.merge(direct_upload: {
|
|
21
|
+
url: blob.service_url_for_direct_upload,
|
|
22
|
+
headers: blob.service_headers_for_direct_upload,
|
|
23
|
+
formData: blob.service_form_data_for_direct_upload
|
|
24
|
+
})
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Serves files stored with the disk service in the same way that the cloud services do.
|
|
4
|
+
# This means using expiring, signed URLs that are meant for immediate access, not permanent linking.
|
|
5
|
+
# Always go through the BlobsController, or your own authenticated controller, rather than directly
|
|
6
|
+
# to the service url.
|
|
7
|
+
class ActiveStorageSaas::DiskController < ActiveStorage::BaseController
|
|
8
|
+
skip_forgery_protection
|
|
9
|
+
|
|
10
|
+
def show
|
|
11
|
+
if key = decode_verified_key
|
|
12
|
+
serve_file disk_service.path_for(key[:key]), content_type: key[:content_type], disposition: key[:disposition]
|
|
13
|
+
else
|
|
14
|
+
head :not_found
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def update
|
|
19
|
+
if token = decode_verified_token
|
|
20
|
+
if acceptable_content?(token)
|
|
21
|
+
disk_service.upload token[:key], request.body, checksum: token[:checksum]
|
|
22
|
+
head :no_content
|
|
23
|
+
else
|
|
24
|
+
head :unprocessable_entity
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
rescue ActiveStorage::IntegrityError
|
|
28
|
+
head :unprocessable_entity
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
def disk_service
|
|
33
|
+
ActiveStorage::Blob.service
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def decode_verified_key
|
|
38
|
+
ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def serve_file(path, content_type:, disposition:)
|
|
42
|
+
Rack::File.new(nil).serving(request, path).tap do |(status, headers, body)|
|
|
43
|
+
self.status = status
|
|
44
|
+
self.response_body = body
|
|
45
|
+
|
|
46
|
+
headers.each do |name, value|
|
|
47
|
+
response.headers[name] = value
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
response.headers["Content-Type"] = content_type || DEFAULT_SEND_FILE_TYPE
|
|
51
|
+
response.headers["Content-Disposition"] = disposition || DEFAULT_SEND_FILE_DISPOSITION
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def decode_verified_token
|
|
57
|
+
ActiveStorage.verifier.verified(params[:encoded_token], purpose: :blob_token)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def acceptable_content?(token)
|
|
61
|
+
token[:content_type] == request.content_mime_type && token[:content_length] == request.content_length
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Take a signed permanent reference for a blob representation and turn it into an expiring service URL for download.
|
|
4
|
+
# Note: These URLs are publicly accessible. If you need to enforce access protection beyond the
|
|
5
|
+
# security-through-obscurity factor of the signed blob and variation reference, you'll need to implement your own
|
|
6
|
+
# authenticated redirection controller.
|
|
7
|
+
class ActiveStorageSaas::RepresentationsController < ActiveStorage::BaseController
|
|
8
|
+
include ActiveStorage::SetBlob
|
|
9
|
+
|
|
10
|
+
def show
|
|
11
|
+
expires_in ActiveStorage::Blob.service.url_expires_in
|
|
12
|
+
redirect_to @blob.representation(params[:variation_key]).processed.service_url(disposition: params[:disposition])
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class ActivestorageSaasTables < ActiveRecord::Migration[5.2]
|
|
2
|
+
def change
|
|
3
|
+
tenant_class = ActiveStorageSaas.tenant_class_name.constantize
|
|
4
|
+
tenant_primary_key = tenant_class.columns.find{|col| col.name == tenant_class.primary_key }
|
|
5
|
+
create_table :tenant_storage_services do |t|
|
|
6
|
+
t.references :tenant, type: tenant_primary_key.type, foreign_key: { to_table: tenant_class.table_name }
|
|
7
|
+
t.string :service_name
|
|
8
|
+
t.json :service_options
|
|
9
|
+
|
|
10
|
+
t.timestamps
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
add_reference tenant_class.table_name.to_sym, :tenant_storage_service, foreign_key: true
|
|
14
|
+
add_reference :active_storage_blobs, :tenant, type: tenant_primary_key.type, foreign_key: true
|
|
15
|
+
add_reference :active_storage_blobs, :tenant_storage_service, foreign_key: true
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
module ActiveStorage
|
|
2
|
+
class Service
|
|
3
|
+
# # config/storage.yml
|
|
4
|
+
# saas:
|
|
5
|
+
# service: Saas
|
|
6
|
+
# default_service: qinium
|
|
7
|
+
# url_expires_in: 300
|
|
8
|
+
# qinium:
|
|
9
|
+
# service: Qinium
|
|
10
|
+
# access_key:
|
|
11
|
+
# secret_key:
|
|
12
|
+
# bucket:
|
|
13
|
+
# put_policy_options:
|
|
14
|
+
# expires_in: 3600
|
|
15
|
+
class SaasService < Service
|
|
16
|
+
attr_reader :blob, :options
|
|
17
|
+
|
|
18
|
+
def initialize(options)
|
|
19
|
+
super()
|
|
20
|
+
@blob = options.delete(:blob)
|
|
21
|
+
@options = options
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
Service.public_instance_methods(false).each do |name|
|
|
25
|
+
define_method name do |*args, &block|
|
|
26
|
+
service.public_send(name, *args, &block)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
redefine_method :url_expires_in do
|
|
31
|
+
@options.fetch(:url_expires_in, 5.minutes)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def method_missing(name, *args, &block)
|
|
35
|
+
service.public_send(name, *args, &block)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def respond_to_missing?(method)
|
|
39
|
+
service.respond_to?(method)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def form_data_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
|
|
43
|
+
if service.respond_to?(:form_data_for_direct_upload)
|
|
44
|
+
service.form_data_for_direct_upload key,
|
|
45
|
+
expires_in: expires_in,
|
|
46
|
+
content_type: content_type,
|
|
47
|
+
content_length: content_length,
|
|
48
|
+
checksum: checksum
|
|
49
|
+
else
|
|
50
|
+
{}
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def analyzers
|
|
55
|
+
service_class = service.class
|
|
56
|
+
service_class.respond_to?(:analyzers) ? service_class.analyzers : []
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def service
|
|
60
|
+
@service ||= blob&.tenant_storage_service&.to_service || default_service
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def default_service
|
|
66
|
+
@default_service ||= ActiveStorage::Service.configure @options[:default_service],
|
|
67
|
+
Rails.configuration.active_storage.service_configurations
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveStorageSaas
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
mattr_accessor :tenant_class_name, default: 'Tenant'
|
|
7
|
+
|
|
8
|
+
require 'active_storage_saas/routes'
|
|
9
|
+
require 'active_storage_saas/blob_patch'
|
|
10
|
+
require 'active_storage_saas/engine'
|
|
11
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module ActiveStorageSaas
|
|
2
|
+
module BlobPatch
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
belongs_to :tenant, class_name: ActiveStorageSaas.tenant_class_name # rubocop: disable Rails/ReflectionClassName
|
|
7
|
+
belongs_to :tenant_storage_service, optional: true
|
|
8
|
+
|
|
9
|
+
redefine_method :service do
|
|
10
|
+
class_service = self.class.service
|
|
11
|
+
if class_service.is_a?(ActiveStorage::Service::SaasService)
|
|
12
|
+
@service ||= ActiveStorage::Service::SaasService.new(class_service.options.merge(blob: self))
|
|
13
|
+
else
|
|
14
|
+
class_service
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# support sending data from form instead of headers
|
|
20
|
+
def service_form_data_for_direct_upload(expires_in: service.url_expires_in)
|
|
21
|
+
return {} unless service.respond_to?(:form_data_for_direct_upload)
|
|
22
|
+
|
|
23
|
+
service.form_data_for_direct_upload(key,
|
|
24
|
+
expires_in: expires_in,
|
|
25
|
+
content_type: content_type,
|
|
26
|
+
content_length: byte_size,
|
|
27
|
+
checksum: checksum)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def saas_service?
|
|
33
|
+
service.is_a?(ActiveStorage::Service::SaasService)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def analyzer_class
|
|
37
|
+
analyzers = saas_service? ? service.analyzers : ActiveStorage.analyzers
|
|
38
|
+
analyzers.detect { |klass| klass.accept?(self) } || ActiveStorage::Analyzer::NullAnalyzer
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module ActiveStorageSaas
|
|
2
|
+
class Engine < Rails::Engine
|
|
3
|
+
config.before_configuration do
|
|
4
|
+
# disable active_storage routes
|
|
5
|
+
ActiveStorage::Engine.config.paths['config/routes.rb'] = []
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
config.to_prepare do
|
|
9
|
+
ActionDispatch::Routing::Mapper.include Routes
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
initializer 'patch' do
|
|
13
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
|
14
|
+
include ActiveStorageSaas::BlobPatch
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module ActiveStorageSaas
|
|
2
|
+
module Routes
|
|
3
|
+
# rubocop: disable Layout/LineLength
|
|
4
|
+
def draw_active_storage_saas_routes(
|
|
5
|
+
prefix: '/rails/active_storage',
|
|
6
|
+
blobs_controller: 'active_storage_saas/blobs',
|
|
7
|
+
representations_controller: 'active_storage_saas/representations',
|
|
8
|
+
disk_controller: 'active_storage_saas/disk',
|
|
9
|
+
direct_uploads_controller: 'active_storage_saas/direct_uploads',
|
|
10
|
+
**option_overrides
|
|
11
|
+
)
|
|
12
|
+
scope prefix do
|
|
13
|
+
get '/blobs/:signed_id/*filename' => "#{blobs_controller}#show", as: :rails_service_blob
|
|
14
|
+
get '/representations/:signed_blob_id/:variation_key/*filename' => "#{representations_controller}#show", as: :rails_blob_representation
|
|
15
|
+
get '/disk/:encoded_key/*filename' => "#{disk_controller}#show", as: :rails_disk_service
|
|
16
|
+
put '/disk/:encoded_token' => "#{disk_controller}#update", as: :update_rails_disk_service
|
|
17
|
+
post '/direct_uploads' => "#{direct_uploads_controller}#create", as: :rails_direct_uploads
|
|
18
|
+
match '/direct_uploads/callback/:signed_id' => "#{direct_uploads_controller}#callback", as: :rails_direct_upload_callback, via: %i[get post]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
direct :rails_blob do |blob, options|
|
|
22
|
+
route_for(:rails_service_blob, blob.signed_id, blob.filename, options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
resolve('ActiveStorage::Blob') { |blob, options| route_for(:rails_blob, blob, options.merge(option_overrides)) }
|
|
26
|
+
resolve('ActiveStorage::Attachment') { |attachment, options| route_for(:rails_blob, attachment.blob, options.merge(option_overrides)) }
|
|
27
|
+
|
|
28
|
+
direct :rails_representation do |representation, options|
|
|
29
|
+
signed_blob_id = representation.blob.signed_id
|
|
30
|
+
variation_key = representation.variation.key
|
|
31
|
+
filename = representation.blob.filename
|
|
32
|
+
|
|
33
|
+
route_for(:rails_blob_representation, signed_blob_id, variation_key, filename, options)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
resolve('ActiveStorage::Variant') { |variant, options| route_for(:rails_representation, variant, options.merge(option_overrides)) }
|
|
37
|
+
resolve('ActiveStorage::Preview') { |preview, options| route_for(:rails_representation, preview, options.merge(option_overrides)) }
|
|
38
|
+
end
|
|
39
|
+
# rubocop: enable Layout/LineLength
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'active_storage_saas'
|
metadata
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activestorage_saas
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 5.2.5.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- xiaohui
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-08-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activestorage
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 5.2.5
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 5.2.5
|
|
27
|
+
description: Each tenant can set its own storage service, ActiveStorage service can
|
|
28
|
+
be dynamically loaded.
|
|
29
|
+
email:
|
|
30
|
+
- xiaohui@tanmer.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- ".rspec"
|
|
36
|
+
- ".rubocop.yml"
|
|
37
|
+
- CHANGELOG.md
|
|
38
|
+
- Gemfile
|
|
39
|
+
- Gemfile.lock
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.md
|
|
42
|
+
- Rakefile
|
|
43
|
+
- activestorage_saas.gemspec
|
|
44
|
+
- app/controller/active_storage_saas/base_controller.rb
|
|
45
|
+
- app/controller/active_storage_saas/blobs_controller.rb
|
|
46
|
+
- app/controller/active_storage_saas/direct_uploads_controller.rb
|
|
47
|
+
- app/controller/active_storage_saas/disk_controller.rb
|
|
48
|
+
- app/controller/active_storage_saas/representations_controller.rb
|
|
49
|
+
- db/migrate/001_activestorage_saas_tables.rb
|
|
50
|
+
- lib/active_storage/service/saas_service.rb
|
|
51
|
+
- lib/active_storage_saas.rb
|
|
52
|
+
- lib/actives_torage_saas/blob_patch.rb
|
|
53
|
+
- lib/actives_torage_saas/engine.rb
|
|
54
|
+
- lib/actives_torage_saas/routes.rb
|
|
55
|
+
- lib/activestorage_saas.rb
|
|
56
|
+
- sig/activestorage_saas.rbs
|
|
57
|
+
homepage: https://github.com/xiaohui-zhangxh/activestorage_saas
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata:
|
|
61
|
+
homepage_uri: https://github.com/xiaohui-zhangxh/activestorage_saas
|
|
62
|
+
source_code_uri: https://github.com/xiaohui-zhangxh/activestorage_saas
|
|
63
|
+
changelog_uri: https://github.com/xiaohui-zhangxh/activestorage_saas/CHANGELOG.md
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: 2.6.0
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubygems_version: 3.2.3
|
|
80
|
+
signing_key:
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: Wraps multi-tenant storage services as ActiveStorage service
|
|
83
|
+
test_files: []
|