aliyunoss-activestorage-adapter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be287d0880ab9d18c34e3368599676a18037c029a4e4c69a3988618e185bcb76
4
+ data.tar.gz: cd9d6c1011ea9033f41b3dc1e14acf879dabaa9d86b25c35847d6008c3fd6e61
5
+ SHA512:
6
+ metadata.gz: 661502c6982f26960218e32091f61718d1a6b3990cb3627cdc531ceb0c8081329ba6677b9eefb2d90191c0f1f34d9ad9755c6964a312af71977d1b457593e095
7
+ data.tar.gz: c955f8cec05d2de4bf93c5aa43cd24e57ac703416ea170d9e04a6cb06617587ea2d642d3ed2d45fb5486f683ee1ff486715385552ca610993dc82c0022b08510
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-18
4
+
5
+ - 完成基础功能
6
+ - 未实现的Service函数:compose, rupload_metadata
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in activestorage-aliyunoss.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "activestorage", "~> 7.0"
15
+
16
+ gem "aliyunoss", "~> 0.2"
data/Gemfile.lock ADDED
@@ -0,0 +1,121 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aliyunoss-activestorage-adapter (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (7.0.4)
10
+ actionview (= 7.0.4)
11
+ activesupport (= 7.0.4)
12
+ rack (~> 2.0, >= 2.2.0)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
16
+ actionview (7.0.4)
17
+ activesupport (= 7.0.4)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
22
+ activejob (7.0.4)
23
+ activesupport (= 7.0.4)
24
+ globalid (>= 0.3.6)
25
+ activemodel (7.0.4)
26
+ activesupport (= 7.0.4)
27
+ activerecord (7.0.4)
28
+ activemodel (= 7.0.4)
29
+ activesupport (= 7.0.4)
30
+ activestorage (7.0.4)
31
+ actionpack (= 7.0.4)
32
+ activejob (= 7.0.4)
33
+ activerecord (= 7.0.4)
34
+ activesupport (= 7.0.4)
35
+ marcel (~> 1.0)
36
+ mini_mime (>= 1.1.0)
37
+ activesupport (7.0.4)
38
+ concurrent-ruby (~> 1.0, >= 1.0.2)
39
+ i18n (>= 1.6, < 2)
40
+ minitest (>= 5.1)
41
+ tzinfo (~> 2.0)
42
+ aliyunoss (0.2.2)
43
+ nokogiri (~> 1.13)
44
+ ast (2.4.2)
45
+ builder (3.2.4)
46
+ concurrent-ruby (1.1.10)
47
+ crass (1.0.6)
48
+ diff-lcs (1.5.0)
49
+ erubi (1.11.0)
50
+ globalid (1.0.0)
51
+ activesupport (>= 5.0)
52
+ i18n (1.12.0)
53
+ concurrent-ruby (~> 1.0)
54
+ json (2.6.3)
55
+ loofah (2.19.1)
56
+ crass (~> 1.0.2)
57
+ nokogiri (>= 1.5.9)
58
+ marcel (1.0.2)
59
+ mini_mime (1.1.2)
60
+ minitest (5.16.3)
61
+ nokogiri (1.13.10-x86_64-linux)
62
+ racc (~> 1.4)
63
+ parallel (1.22.1)
64
+ parser (3.1.3.0)
65
+ ast (~> 2.4.1)
66
+ racc (1.6.1)
67
+ rack (2.2.4)
68
+ rack-test (2.0.2)
69
+ rack (>= 1.3)
70
+ rails-dom-testing (2.0.3)
71
+ activesupport (>= 4.2.0)
72
+ nokogiri (>= 1.6)
73
+ rails-html-sanitizer (1.4.4)
74
+ loofah (~> 2.19, >= 2.19.1)
75
+ rainbow (3.1.1)
76
+ rake (13.0.6)
77
+ regexp_parser (2.6.1)
78
+ rexml (3.2.5)
79
+ rspec (3.12.0)
80
+ rspec-core (~> 3.12.0)
81
+ rspec-expectations (~> 3.12.0)
82
+ rspec-mocks (~> 3.12.0)
83
+ rspec-core (3.12.0)
84
+ rspec-support (~> 3.12.0)
85
+ rspec-expectations (3.12.1)
86
+ diff-lcs (>= 1.2.0, < 2.0)
87
+ rspec-support (~> 3.12.0)
88
+ rspec-mocks (3.12.1)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.12.0)
91
+ rspec-support (3.12.0)
92
+ rubocop (1.40.0)
93
+ json (~> 2.3)
94
+ parallel (~> 1.10)
95
+ parser (>= 3.1.2.1)
96
+ rainbow (>= 2.2.2, < 4.0)
97
+ regexp_parser (>= 1.8, < 3.0)
98
+ rexml (>= 3.2.5, < 4.0)
99
+ rubocop-ast (>= 1.23.0, < 2.0)
100
+ ruby-progressbar (~> 1.7)
101
+ unicode-display_width (>= 1.4.0, < 3.0)
102
+ rubocop-ast (1.24.0)
103
+ parser (>= 3.1.1.0)
104
+ ruby-progressbar (1.11.0)
105
+ tzinfo (2.0.5)
106
+ concurrent-ruby (~> 1.0)
107
+ unicode-display_width (2.3.0)
108
+
109
+ PLATFORMS
110
+ x86_64-linux
111
+
112
+ DEPENDENCIES
113
+ activestorage (~> 7.0)
114
+ aliyunoss (~> 0.2)
115
+ aliyunoss-activestorage-adapter!
116
+ rake (~> 13.0)
117
+ rspec (~> 3.0)
118
+ rubocop (~> 1.21)
119
+
120
+ BUNDLED WITH
121
+ 2.3.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Your Name
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,55 @@
1
+ # ActiveStorage的阿里云存储适配器
2
+
3
+ 该Gem是一个Aliyun OSS插件,可为Rails 7.0的Active Storage添加阿里云对象存储接口。
4
+
5
+ ## 安装方法
6
+
7
+ 在Rails工程中的Gemfile中添加以下代码:
8
+
9
+ gem "aliyunoss-activestorage-adapter", "~> 0.1"
10
+
11
+ 然后运行bundle install即可。
12
+
13
+ ## 使用方法
14
+
15
+ ### 配置文件
16
+
17
+ Active Storage的配置文件在/config/storage.yml文件中,请添加以下配置信息:
18
+
19
+ aliyun:
20
+ service: "Aliyunoss"
21
+
22
+ # 阿里云开发者信息
23
+ access_key_id: <%= ENV["ALI_ACCESS_KEY"] %>
24
+ access_key_secret: <%= ENV["ALI_ACCESS_SECRET"] %>
25
+
26
+ # Bucket名称
27
+ bucket: "[Bucket_Name]"
28
+
29
+ # Bucket位置
30
+ location: "oss-cn-beijing"
31
+
32
+ # 根目录
33
+ path: "/"
34
+
35
+ # 根据创建的Bucket访问类型选择
36
+ is_public: true
37
+
38
+ ### 调用方法
39
+
40
+ 配置完成后,可以按照[Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html)手册调用。
41
+
42
+ ## 开发
43
+
44
+ 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.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yijiecc/activestorage-aliyunoss. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yijiecc/activestorage-aliyunoss/blob/master/CODE_OF_CONDUCT.md).
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
55
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/active_storage/service/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "aliyunoss-activestorage-adapter"
7
+ spec.version = ActiveStorage::AliyunossService::VERSION
8
+ spec.authors = ["yiiecc"]
9
+ spec.email = ["yijiecc@hotmail.com"]
10
+
11
+ spec.summary = "A plugin that enables Rails App using Aliyun OSS."
12
+ spec.description = "A plugin that enables Rails App using Aliyun OSS, this gem depends on aliyunoss gem."
13
+ spec.homepage = "https://rubygems.org/gems/aliyunoss-activestorage-adapter"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "https://github.com/yijiecc/activestorage-aliyunoss"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/yijiecc/activestorage-aliyunoss"
21
+ spec.metadata["changelog_uri"] = "https://github.com/yijiecc/activestorage-aliyunoss/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "service/version"
4
+ require_relative "service/aliyunoss_service"
5
+
6
+
@@ -0,0 +1,152 @@
1
+ require 'active_storage'
2
+ require 'aliyunoss'
3
+
4
+ module ActiveStorage
5
+ class Service::AliyunossService < Service
6
+
7
+ #
8
+ # keyword arguments:
9
+ # {
10
+ # access_key_id: <%= ENV["ALI_ACCESS_KEY"] %>
11
+ # access_key_secret: <%= ENV["ALI_ACCESS_SECRET"] %>
12
+ # bucket: "[BUCKET_NAME]"
13
+ # location: "oss-cn-beijing"
14
+ # path: "[PATH]"
15
+ # is_public: true
16
+ # }
17
+ #
18
+ def initialize(bucket:, access_key_id:, access_key_secret:, location:, path:, is_public:)
19
+
20
+ Aliyun::Oss::configure(
21
+ :access_key_id => access_key_id,
22
+ :access_key_secret => access_key_secret
23
+ )
24
+
25
+ @bucket = Aliyun::Oss::Bucket.new(name: bucket, location: location)
26
+ @base_path = path
27
+ @public = is_public
28
+
29
+ end
30
+
31
+ # Upload the +io+ to the +key+ specified. If a +checksum+ is provided, the service will
32
+ # ensure a match when the upload has completed or raise an ActiveStorage::IntegrityError.
33
+ # aliyunoss gem will calculate checksum again, so this is useless
34
+ def upload(key, io, checksum: nil, **options)
35
+ if checksum != nil
36
+ options_with_checksum = options.merge('Content-MD5': checksum)
37
+ @bucket.upload(io.read, path_for(key), options_with_checksum)
38
+ else
39
+ @bucket.upload(io.read, path_for(key), options)
40
+ end
41
+ end
42
+
43
+ # Update metadata for the file identified by +key+ in the service.
44
+ # Override in subclasses only if the service needs to store specific
45
+ # metadata that has to be updated upon identification.
46
+ def update_metadata(key, **metadata)
47
+ raise NotImplementedError
48
+ end
49
+
50
+ # Return the content of the file at the +key+.
51
+ def download(key)
52
+ @bucket.download(path_for(key))
53
+ end
54
+
55
+ # Return the partial content in the byte +range+ of the file at the +key+.
56
+ def download_chunk(key, range)
57
+ @bucket.download(path_for(key), Range: range)
58
+ end
59
+
60
+ # Concatenate multiple files into a single "composed" file.
61
+ def compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {})
62
+ raise NotImplementedError
63
+ end
64
+
65
+ # Delete the file at the +key+.
66
+ def delete(key)
67
+ @bucket.delete(path_for(key))
68
+ end
69
+
70
+ # Delete files at keys starting with the +prefix+.
71
+ def delete_prefixed(prefix)
72
+ @bucket.list_files.each do |obj|
73
+ file_name = obj.fetch('key', '')
74
+ file_name = '/' + file_name if file_name != ''
75
+ @bucket.delete(file_name) if file_name.start_with?(File.join(@base_path, prefix))
76
+ end
77
+ end
78
+
79
+ # Return +true+ if a file exists at the +key+.
80
+ def exist?(key)
81
+ @bucket.exist? path_for(key)
82
+ end
83
+
84
+ # Returns the URL for the file at the +key+. This returns a permanent URL for public files, and returns a
85
+ # short-lived URL for private files. For private files you can provide
86
+ # the amount of seconds the URL will be valid for, specified in +expires_in+.
87
+ def url(key, **options)
88
+ instrument :url, key: key do |payload|
89
+ generated_url =
90
+ if public?
91
+ public_url(key, **options)
92
+ else
93
+ private_url(key, **options)
94
+ end
95
+
96
+ payload[:url] = generated_url
97
+
98
+ generated_url
99
+ end
100
+ end
101
+
102
+ # Returns a signed, temporary URL that a direct upload file can be PUT to on the +key+.
103
+ # The URL will be valid for the amount of seconds specified in +expires_in+.
104
+ # You must also provide the +content_type+, +content_length+, and +checksum+ of the file
105
+ # that will be uploaded. All these attributes will be validated by the service upon upload.
106
+ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
107
+ raise NotImplementedError
108
+ end
109
+
110
+ # Returns a Hash of headers for +url_for_direct_upload+ requests.
111
+ def headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:, custom_metadata: {})
112
+ {}
113
+ end
114
+
115
+ def public?
116
+ @public
117
+ end
118
+
119
+ private
120
+ def path_for(key)
121
+ File.join(@base_path, key)
122
+ end
123
+
124
+ def private_url(key, expires_in:, **)
125
+ @bucket.share(path_for(key), expires_in)
126
+ end
127
+
128
+ def public_url(key, **)
129
+ "https://#{@bucket}.#{@location}.aliyuncs.com#{path_for(key)}"
130
+ end
131
+
132
+ def custom_metadata_headers(metadata)
133
+ raise NotImplementedError
134
+ end
135
+
136
+ def instrument(operation, payload = {}, &block)
137
+ ActiveSupport::Notifications.instrument(
138
+ "service_#{operation}.active_storage",
139
+ payload.merge(service: service_name), &block)
140
+ end
141
+
142
+ def service_name
143
+ "Aliyunoss"
144
+ end
145
+
146
+ def content_disposition_with(type: "inline", filename:)
147
+ disposition = (type.to_s.presence_in(%w( attachment inline )) || "inline")
148
+ ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: filename.sanitized)
149
+ end
150
+
151
+ end
152
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveStorage
4
+ module AliyunossService
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Activestorage
2
+ module Aliyunoss
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyunoss-activestorage-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yiiecc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A plugin that enables Rails App using Aliyun OSS, this gem depends on
14
+ aliyunoss gem.
15
+ email:
16
+ - yijiecc@hotmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - activestorage-aliyunoss.gemspec
30
+ - lib/active_storage/aliyunoss.rb
31
+ - lib/active_storage/service/aliyunoss_service.rb
32
+ - lib/active_storage/service/version.rb
33
+ - sig/activestorage/aliyunoss.rbs
34
+ homepage: https://rubygems.org/gems/aliyunoss-activestorage-adapter
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://rubygems.org/gems/aliyunoss-activestorage-adapter
39
+ source_code_uri: https://github.com/yijiecc/activestorage-aliyunoss
40
+ changelog_uri: https://github.com/yijiecc/activestorage-aliyunoss/blob/main/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.3.7
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A plugin that enables Rails App using Aliyun OSS.
60
+ test_files: []