activestorage-aliyun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 280e83ae1965f9a922c30d70f38be9ed550abaa4
4
+ data.tar.gz: b6c1f47cf9f9445309f7efe09578047396fdde37
5
+ SHA512:
6
+ metadata.gz: 7e6c52b08d031f69fb05fa444edf7906ea76a18267874e0043caceb6c53127454a357c98cf759cfc16b1215e453893331ffb1970972412f6b6c8b2767fe7a51d
7
+ data.tar.gz: 55fc172b6b021a02f8aa3f7096a2f2de9324008c0c3e12080ca22f02bbe53a85fcb65ba245fe8572b94ee85224f64c9b1e6d5d47c49c83ad44cf2424136115b1
@@ -0,0 +1,28 @@
1
+ # ActivestorageAliyun
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'activestorage-aliyun'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install activestorage-aliyun
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiveStorage Aliyun Service'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+ require "aliyun/oss"
3
+ module ActiveStorage
4
+ class Service::AliyunService < Service
5
+ def initialize(**config)
6
+ Aliyun::Common::Logging.set_log_file("/dev/null")
7
+ @config = config
8
+ end
9
+
10
+ def upload(key, io, checksum: nil)
11
+ headers = {}
12
+ instrument :upload, key: key, checksum: checksum do
13
+ bucket.put_object(path_for(key), file: io)
14
+ end
15
+ end
16
+
17
+ def download(key)
18
+ instrument :download, key: key do
19
+ bucket.get_object(path_for(key))
20
+ end
21
+ end
22
+
23
+ def delete(key)
24
+ instrument :delete, key: key do
25
+ bucket.delete_object(path_for(key))
26
+ end
27
+ end
28
+
29
+ def delete_prefixed(prefix)
30
+ instrument :delete_prefixed, prefix: prefix do
31
+ bucket.delete_object(path_for(prefix))
32
+ end
33
+ end
34
+
35
+ def exist?(key)
36
+ instrument :exist, key: key do |payload|
37
+ bucket.object_exists?(path_for(key))
38
+ end
39
+ end
40
+
41
+ def url(key, expires_in:, filename:, content_type:, disposition:)
42
+ instrument :url, key: key do |payload|
43
+ generated_url = bucket.object_url(path_for(key), false, expires_in)
44
+ generated_url.gsub('http://', 'https://')
45
+ if filename.present?
46
+ generated_url = [generated_url, filename].join("?")
47
+ end
48
+
49
+ payload[:url] = generated_url
50
+
51
+ generated_url
52
+ end
53
+ end
54
+
55
+ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
56
+ instrument :url, key: key do |payload|
57
+ # FIXME: to implement direct upload
58
+ raise 'Not implement'
59
+ end
60
+ end
61
+
62
+ def headers_for_direct_upload(key, content_type:, checksum:, **)
63
+ { "Content-Type" => content_type, "Content-MD5" => checksum }
64
+ end
65
+
66
+ private
67
+ attr_reader :config
68
+
69
+ def path_for(key)
70
+ return key if !config.fetch(:path, nil)
71
+ File.join(config.fetch(:path), key)
72
+ end
73
+
74
+ def bucket
75
+ return @bucket if defined? @bucket
76
+ @bucket = client.get_bucket(config.fetch(:bucket))
77
+ @bucket
78
+ end
79
+
80
+ def endpoint
81
+ config.fetch(:endpoint, "https://oss-cn-hangzhou.aliyuncs.com")
82
+ end
83
+
84
+ def client
85
+ @client ||= client = Aliyun::OSS::Client.new(
86
+ endpoint: endpoint,
87
+ access_key_id: config.fetch(:access_key_id),
88
+ access_key_secret: config.fetch(:access_key_secret),
89
+ cname: config.fetch(:cname, false)
90
+ )
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveStorageAliyun
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveStorageAliyun
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require "active_storage_aliyun/railtie"
2
+
3
+ module ActiveStorageAliyun
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activestorage-aliyun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-01 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: 5.2.0.rc
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.0.rc
27
+ - !ruby/object:Gem::Dependency
28
+ name: aliyun-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ description: Wraps the Aliyun OSS as an Active Storage service.
42
+ email: huacnlee@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.md
48
+ - Rakefile
49
+ - lib/active_storage/service/aliyun_service.rb
50
+ - lib/active_storage_aliyun/railtie.rb
51
+ - lib/active_storage_aliyun/version.rb
52
+ - lib/activestorage-aliyun.rb
53
+ homepage: https://github.com/huacnlee/activestorage-aliyun
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.6.13
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Wraps the Aliyun OSS as an Active Storage service
77
+ test_files: []