qiniu_rails 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: 951bcaaabf4916cde148a73220655fca777830f85a6a84452431031189fe7327
4
+ data.tar.gz: c8d16a925bd663d5db119eb4003ead716287f04a31f26ad15d3e3fa28064f6ff
5
+ SHA512:
6
+ metadata.gz: 4418d2b01656effd8fd64f15c7e8f831459b33e7b1cd0c2caa2182d15dea2eddc954b5c66cc9df3a2c356d552d3b2d69133e1659ac22ccb8df5a8bc35b3d45b6
7
+ data.tar.gz: bd32f965f8d6107aeaf8d25af920566701f46bda8b10a7463338a556abe975395202a2835bd49881bf535e16e98107f2766061182cc4a75e43e2d0e7c4456933
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 qinmingyuan
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,38 @@
1
+ # ActivestorageQiniu
2
+ qiniu backend for activestore
3
+
4
+
5
+ ## Usage
6
+
7
+
8
+ ```yaml
9
+ qiniu:
10
+ service: Qiniu
11
+ host: xxxx.com1.z0.glb.clouddn.com
12
+ access_key: iX6NuM1xN04Wdh-DogI0F3jLVpc-A4CsTHETssss
13
+ secret_key: aN44R3yzJFaeswbyM4Y8YaJvnkmsssssssss
14
+ bucket: xxxx
15
+ ```
16
+
17
+ ## Installation
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'activestorage_qiniu'
22
+ ```
23
+
24
+ And then execute:
25
+ ```bash
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+ ```bash
31
+ $ gem install activestorage_qiniu
32
+ ```
33
+
34
+ ## Contributing
35
+ Contribution directions go here.
36
+
37
+ ## License
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
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 = 'ActivestorageQiniu'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ require 'activestorage_qiniu/qiniu_common'
3
+
4
+ module ActiveStorage
5
+ # Wraps the Qiniu Cloud Storage as an Active Storage service. See ActiveStorage::Service for the generic API
6
+ # documentation that applies to all services.
7
+ class Service::QiniuService < Service
8
+ include QiniuCommon
9
+ attr_reader :client
10
+
11
+ def initialize(host:, secret_key:, access_key:, bucket:, **options)
12
+ @client = Qiniu.establish_connection!(access_key: access_key, secret_key: secret_key)
13
+ @host = host
14
+ @bucket = bucket
15
+ end
16
+
17
+ def upload(key, io, checksum: nil, **options)
18
+ instrument :upload, key: key, checksum: checksum do
19
+ begin
20
+ code, result, response_headers = upload_verbose(io, key, options)
21
+ result['key']
22
+ rescue => e
23
+ puts e.backtrace
24
+ raise ActiveStorage::IntegrityError
25
+ end
26
+ end
27
+ end
28
+
29
+ def delete(key)
30
+ instrument :delete, key: key do
31
+ begin
32
+ Qiniu::Storage.delete(bucket, key)
33
+ rescue => e
34
+ puts e.backtrace
35
+ end
36
+ end
37
+ end
38
+
39
+ def exist?(key)
40
+ instrument :exist, key: key do |payload|
41
+ answer = file_for(key)
42
+ payload[:exist] = answer
43
+ end
44
+ end
45
+
46
+ def url(key, **options)
47
+ instrument :url, key: key do |payload|
48
+ Qiniu::Auth.authorize_download_url_2(host, key)
49
+ end
50
+ end
51
+
52
+ def headers_for_direct_upload(key, content_type:, checksum:, **)
53
+ { 'Content-Type' => content_type, 'Content-MD5' => checksum, 'x-token' => generate_uptoken(key) }
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ require 'qiniu'
3
+ module QiniuCommon
4
+ attr_reader :host, :bucket
5
+
6
+ private
7
+ def upload_verbose(local_file, key = nil, **options)
8
+ code, result, response_headers = Qiniu::Storage.upload_with_token_2(
9
+ generate_uptoken(key, options),
10
+ local_file,
11
+ key,
12
+ nil,
13
+ bucket: bucket
14
+ )
15
+ end
16
+
17
+ def file_for(prefix = '')
18
+ list_policy = Qiniu::Storage::ListPolicy.new(bucket, 10, prefix, '/')
19
+ code, result, response_headers, s, d = Qiniu::Storage.list(list_policy)
20
+ result['items']
21
+ end
22
+
23
+ def generate_uptoken(key = nil, expires_in: Qiniu::Auth::DEFAULT_AUTH_SECONDS, deadline: nil, **options)
24
+ put_policy = Qiniu::Auth::PutPolicy.new(bucket, key, expires_in, deadline)
25
+ options.slice(*Qiniu::Auth::PutPolicy::PARAMS.keys).each do |k, v|
26
+ put_policy.send("#{k}=", v)
27
+ end
28
+ Qiniu::Auth.generate_uptoken(put_policy)
29
+ end
30
+
31
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require 'qiniu'
3
+ require 'activestorage_qiniu'
4
+
5
+ module QiniuHelper
6
+ extend QiniuCommon
7
+ extend self
8
+ @config ||= Rails.configuration.active_storage['service_configurations']['qiniu']
9
+ @host ||= @config['host']
10
+ @bucket ||= @config['bucket']
11
+
12
+ def download_url(key)
13
+ Qiniu::Auth.authorize_download_url_2(host, key)
14
+ end
15
+
16
+ def qiniu_url(key)
17
+ _host = host
18
+ _host = host + '/' unless _host.end_with? '/'
19
+ _host = 'http://' + _host unless _host.start_with? 'http://'
20
+ _host + key.to_s
21
+ end
22
+
23
+ def upload(local_file, key = nil, **options)
24
+ code, result, response_headers = upload_verbose(local_file, key, options)
25
+ result['key']
26
+ end
27
+
28
+ def delete(key)
29
+ code, result, response_headers = Qiniu::Storage.delete(
30
+ bucket,
31
+ key
32
+ )
33
+ code
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module QiniuRails
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_storage/service/qiniu_service'
2
+ autoload :QiniuHelper, 'qiniu_rails/qiniu_helper'
3
+ module QiniuRails
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activestorage_qiniu do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qiniu_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - qinmingyuan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-18 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.0.rc1
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.rc1
27
+ description: Description of ActivestorageQiniu.
28
+ email:
29
+ - mingyuan0715@foxmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/active_storage/service/qiniu_service.rb
38
+ - lib/qiniu_rails.rb
39
+ - lib/qiniu_rails/qiniu_common.rb
40
+ - lib/qiniu_rails/qiniu_helper.rb
41
+ - lib/qiniu_rails/version.rb
42
+ - lib/tasks/activestorage_qiniu_tasks.rake
43
+ homepage: https://github.com/qinmingyuan/qiniu_rails
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.7.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: qiniu backend for activestore
67
+ test_files: []