paperclip-storage-aliyun 0.1.2 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4f45c4d78df19bc94d47a95df25828e4dc748bb3
4
- data.tar.gz: eef3b058ae65e4bac93803f72a2117a90d22cf83
2
+ SHA256:
3
+ metadata.gz: 25b96669f6b0a9f6c7f727cecd1ef74045f3a69aa0ace67b0558b6c7f15fb358
4
+ data.tar.gz: 2ba4caf9e55a44037772e7059075cce390dcf27f6a903b052d058dd554d07189
5
5
  SHA512:
6
- metadata.gz: '00985474f87c15bcc703e40e10db405b4f63a58348f84be779b05e4651bf303862b2457b8f3f27fb74f8550e31d422771d178eb370a30cd55b88be57e6650b5a'
7
- data.tar.gz: cab455de6ebfbcfc7e5edfede4344774bb47381e42faad45af2c5549704a22bbbb7226fc71c54f7828419a09ff3b9257e7a1c609e3a9d6b2d2ab20138271dbd1
6
+ metadata.gz: d46717fbd0418be321ae684a2ecc64c405fb4d51a8b265041896a924fa8994721962ba346902e9a95bb790a5b3cb9513a2bc05a63e0beddf2fe7f7dd377d166f
7
+ data.tar.gz: 1dc297fe30701431f94144ac5e08604f9181b45b188cf9f719dd30529b309a83cc5be8560fb5f54f3ecfe9bbd3ccfb48e3e5370e3ca2178300ec016da4c800e3
@@ -26,7 +26,7 @@ module Aliyun
26
26
  # @option access_id [String] used to set "Authorization" request header
27
27
  # @option access_key [String] the access key
28
28
  # @option bucket [String] bucket used to access
29
- # @option data_center [String] available data center name, e.g. 'hangzhou'
29
+ # @option data_center [String] available data center name, e.g. 'cn-hangzhou'
30
30
  # @option internal [true, false] if the service should be accessed through internal network
31
31
  # @option host_alias [String] the alias of the host, such as the CDN domain name
32
32
  # @note both access_id and acces_key are related to authorization algorithm:
@@ -44,7 +44,7 @@ module Aliyun
44
44
  @aliyun_alias_host = options[:host_alias] || @aliyun_upload_host
45
45
  end
46
46
 
47
- # Return the meta informations for the a file specified by the path
47
+ # Return the meta informations for a file specified by the path
48
48
  # https://docs.aliyun.com/#/pub/oss/api-reference/object&HeadObject
49
49
  #
50
50
  # @param path [String] the path of file storaged in Aliyun OSS
@@ -64,8 +64,16 @@ module Aliyun
64
64
  # :x_oss_request_id=>"55BD83A5D4C05BDFF4A329E0"}}
65
65
  #
66
66
  def head(path)
67
+ path = format_path(path)
68
+ bucket_path = get_bucket_path(path)
69
+ date = gmtdate
70
+ headers = {
71
+ 'Host' => @aliyun_upload_host,
72
+ 'Date' => date,
73
+ 'Authorization' => sign('HEAD', bucket_path, '', '', date)
74
+ }
67
75
  url = path_to_url(path)
68
- RestClient.head(url).headers
76
+ RestClient.head(url, headers).headers
69
77
  rescue RestClient::ResourceNotFound
70
78
  {}
71
79
  end
@@ -5,30 +5,36 @@ module Aliyun
5
5
  # https://docs.aliyun.com/#/pub/oss/product-documentation/domain-region
6
6
  AVAILABLE_DATA_CENTERS = %w(
7
7
  oss-cn-hangzhou
8
+ oss-cn-shanghai
8
9
  oss-cn-qingdao
9
10
  oss-cn-beijing
10
- oss-cn-hongkong
11
+ oss-cn-zhangjiakou
12
+ oss-cn-huhehaote
11
13
  oss-cn-shenzhen
12
- oss-cn-shanghai
14
+ oss-cn-hongkong
13
15
  oss-us-west-1
16
+ oss-us-east-1
14
17
  oss-ap-southeast-1
18
+ oss-ap-southeast-2
19
+ oss-ap-southeast-3
20
+ oss-ap-southeast-5
21
+ oss-ap-northeast-1
22
+ oss-ap-south-1
23
+ oss-eu-central-1
24
+ oss-eu-west-1
25
+ oss-me-east-1
15
26
  )
16
27
 
17
28
  def get_endpoint(options)
18
- data_center = find_center(options[:data_center])
29
+ data_center = options[:data_center]
19
30
 
20
- unless data_center && AVAILABLE_DATA_CENTERS.include?(data_center)
31
+ data_center.prepend('oss-') unless data_center.match(/^oss/)
32
+
33
+ unless AVAILABLE_DATA_CENTERS.include?(data_center)
21
34
  fail InvalildDataCenter, "Unsupported Data Center #{options[:data_center]} Detected"
22
35
  end
23
36
 
24
37
  "#{data_center}#{options[:internal] ? '-internal' : ''}.aliyuncs.com"
25
38
  end
26
-
27
- def find_center(data_center)
28
- return if /^(oss|cn|us|ap|oss-cn)$/.match(data_center)
29
-
30
- regexp = Regexp.new(data_center)
31
- AVAILABLE_DATA_CENTERS.find { |center| regexp.match(center) }
32
- end
33
39
  end
34
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-storage-aliyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Hong
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-19 00:00:00.000000000 Z
12
+ date: 2019-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: paperclip
@@ -45,27 +45,11 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - ".editorconfig"
49
- - ".gitignore"
50
- - ".rspec"
51
- - ".rubocop.yml"
52
- - Gemfile
53
- - README.md
54
48
  - lib/aliyun/connection.rb
55
49
  - lib/aliyun/data_center.rb
56
50
  - lib/aliyun/errors.rb
57
51
  - lib/paperclip-storage-aliyun.rb
58
52
  - lib/paperclip/storage/aliyun.rb
59
- - paperclip-storage-aliyun.gemspec
60
- - spec/aliyun_spec.rb
61
- - spec/attachments/girl.jpg
62
- - spec/attachments/masu.pdf
63
- - spec/attachments/美女.jpg
64
- - spec/fixtures/schema.rb
65
- - spec/lib/paperclip/storage/aliyun_spec.rb
66
- - spec/spec_helper.rb
67
- - spec/support/post.rb
68
- - spec/support/rails.rb
69
53
  homepage: https://github.com/Martin91/paperclip-storage-aliyun
70
54
  licenses:
71
55
  - MIT
@@ -85,8 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
69
  - !ruby/object:Gem::Version
86
70
  version: '0'
87
71
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.6.6
72
+ rubygems_version: 3.0.1
90
73
  signing_key:
91
74
  specification_version: 4
92
75
  summary: Extend a Aliyun OSS storage for paperclip
data/.editorconfig DELETED
@@ -1,13 +0,0 @@
1
- # EditorConfig is awesome: http://EditorConfig.org
2
-
3
- # top-most EditorConfig file
4
- root = true
5
-
6
- # Unix-style newlines with a newline ending every file
7
- [*]
8
- charset = utf-8
9
- indent_style = space
10
- indent_size = 2
11
- end_of_line = lf
12
- insert_final_newline = true
13
- trim_trailing_whitespace = true
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- Gemfile.lock
2
- .yardoc/
3
- doc/
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/.rubocop.yml DELETED
@@ -1,11 +0,0 @@
1
- Style/Documentation:
2
- Enabled: false
3
-
4
- Metrics/AbcSize:
5
- Max: 18
6
-
7
- Metrics/LineLength:
8
- Max: 120
9
-
10
- Metrics/MethodLength:
11
- Enabled: false
data/Gemfile DELETED
@@ -1,16 +0,0 @@
1
- source 'https://gems.ruby-china.org'
2
-
3
- gemspec
4
-
5
- group :test do
6
- gem 'activerecord', '~> 4.0.0'
7
- # A database backend that translates database interactions into no-ops. Using
8
- # NullDB enables you to test your model business logic - including after_save
9
- # hooks - without ever touching a real database.
10
- gem 'activerecord-nulldb-adapter'
11
- gem 'rspec', '~> 3.3.0'
12
- # Use rubocop to lint our Ruby codes
13
- gem 'rubocop', '~> 0.34.2'
14
- gem 'pry'
15
- gem 'pry-nav'
16
- end
data/README.md DELETED
@@ -1,65 +0,0 @@
1
- Aliyun Open Storage Service for Paperclip
2
- ===
3
- This gem implement the support for [Aliyun open storage service(OSS)](http://oss.aliyun.com) to [Paperclip](https://github.com/thoughtbot/paperclip).
4
-
5
- #### Installation
6
- ```shell
7
- gem install paperclip-storage-aliyun
8
- ```
9
- Or, if you are using a bundler, you can append the following line into your **Gemfile**:
10
- ```ruby
11
- gem 'paperclip-storage-aliyun'
12
- ```
13
-
14
- #### Configuration
15
- In order to make all the things work, you should do some important configurations through a initializer:
16
-
17
- If you are developing a Rails application, you can append a new initializer like:
18
- ```ruby
19
- # [rails_root]/config/initializers/paperclip-aliyun-configuration.rb
20
- Paperclip::Attachment.default_options[:aliyun] = {
21
- access_id: '3VL9XMho8iCushj8',
22
- access_key: 'VAUI2q7Tc6yTh1jr3kBsEUzZ84gEa2',
23
- bucket: 'xx-test',
24
- data_center: 'hangzhou',
25
- internal: false,
26
- protocol: 'https'
27
- }
28
- ```
29
- Then, in the model which defines the attachment, specify your storage and other options, for example:
30
- ```ruby
31
- # [rails_root]/app/models/image.rb
32
- class Image < ActiveRecord::Base
33
- has_attached_file :attachment, {
34
- storage: :aliyun,
35
- styles: { thumbnail: "60x60#"},
36
- path: 'public/system/:class/:attachment/:id_partition/:style/:filename',
37
- url: ':aliyun_upload_url'
38
- }
39
- end
40
- ```
41
-
42
- Similar to Paperclip::Storage::S3, there are four options for the url by now:
43
- - `:aliyun_upload_url` : the url based on the options you give
44
- - `:aliyun_internal_url` : the internal url, no matter what `options[:aliyun][:internal]` is
45
- - `:aliyun_external_url` : the external url, no matter what `options[:aliyun][:internal]` is
46
- - `:aliyun_alias_url` : the alias url based on the `host_alias` you give, typically used together with CDN
47
-
48
- Please note the values above are all strings, not symbols. You could still make your own url if only you know what you are doing.
49
-
50
- #### Test
51
- 1. Update connection settings in `spec/spec_helper.rb`:
52
-
53
- ```ruby
54
- # Aliyun defaults
55
- OSS_CONNECTION_OPTIONS = {
56
- access_id: 'your_access_key_id',
57
- access_key: 'your_access_key_secret',
58
- bucket: 'your_bucket',
59
- data_center: 'your_data_center',
60
- internal: false,
61
- protocol: 'https'
62
- }
63
- ```
64
-
65
- 2. Run `bundle exec rspec spec`.
@@ -1,16 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = 'paperclip-storage-aliyun'
3
- s.platform = Gem::Platform::RUBY
4
- s.require_path = 'lib'
5
- s.summary = 'Extend a Aliyun OSS storage for paperclip'
6
- s.description = 'Extend a Aliyun OSS storage for paperclip'
7
- s.version = '0.1.2'
8
- s.files = `git ls-files`.split("\n")
9
- s.authors = ['Martin Hong', 'Aidi Stan']
10
- s.email = 'hongzeqin@gmail.com'
11
- s.homepage = 'https://github.com/Martin91/paperclip-storage-aliyun'
12
- s.license = 'MIT'
13
-
14
- s.add_runtime_dependency 'paperclip', '>= 3.5.2'
15
- s.add_runtime_dependency 'rest-client', '>= 1.6.7'
16
- end
data/spec/aliyun_spec.rb DELETED
@@ -1,77 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'net/http'
4
-
5
- describe Aliyun::Connection do
6
- before :all do
7
- @connection = ::Aliyun::Connection.new OSS_CONNECTION_OPTIONS
8
- @path = 'a/a.jpg'
9
- end
10
-
11
- describe '#initialize' do
12
- it 'raise error when use invalid data center' do
13
- expect do
14
- ::Aliyun::Connection.new data_center: 'guangzhou'
15
- end.to raise_error(Aliyun::InvalildDataCenter)
16
- end
17
- end
18
-
19
- describe '#put' do
20
- it 'upload the attachment' do
21
- url = @connection.put @path, load_attachment('girl.jpg')
22
- response_code = Net::HTTP.get_response(URI.parse(url)).code
23
- expect(response_code).to eq('200')
24
- end
25
-
26
- it 'support setting content type' do
27
- content_type = 'application/pdf'
28
- path = 'pdfs/masu.pdf'
29
- @connection.put path, load_attachment('masu.pdf'), content_type: content_type
30
- file_meta = @connection.head(path)
31
- expect(file_meta[:content_type]).to eq(content_type)
32
-
33
- @connection.delete path
34
- end
35
- end
36
-
37
- describe '#delete' do
38
- it 'delete the attachment' do
39
- url = @connection.delete @path
40
- response_code = Net::HTTP.get_response(URI.parse(url)).code
41
- expect(response_code).to eq('404')
42
- end
43
-
44
- describe "delete attachment with Chinese name" do
45
- it "delete the attachment" do
46
- path = "a/美女.jpg"
47
- @connection.put path, load_attachment('美女.jpg')
48
- url = @connection.delete path
49
- response_code = Net::HTTP.get_response(URI.parse(url)).code
50
- expect(response_code).to eq('404')
51
- end
52
- end
53
- end
54
-
55
- describe '#exists?' do
56
- before :all do
57
- @connection.put @path, load_attachment('girl.jpg')
58
- @path_include_chinese = "美女.jpg"
59
- @connection.put @path_include_chinese, load_attachment("美女.jpg")
60
- end
61
-
62
- it 'return true if the file has been uploaded' do
63
- expect(@connection.exists?(@path)).to be_truthy
64
- end
65
-
66
- it "return false if the specified file didn't exist" do
67
- @connection.delete @path
68
- expect(@connection.exists?(@path)).to be_falsey
69
- end
70
-
71
- it "also return true for existed file with path include chinese characters" do
72
- expect(@connection.exists?(@path_include_chinese)).to be_truthy
73
- url = "#{@connection.aliyun_protocol}://#{@connection.aliyun_upload_host}/#{@path_include_chinese}"
74
- expect(@connection.exists?(url)).to be_truthy
75
- end
76
- end
77
- end
Binary file
Binary file
@@ -1,8 +0,0 @@
1
- require 'active_record'
2
- require 'paperclip'
3
-
4
- ActiveRecord::Schema.define do
5
- create_table :posts, force: true do |t|
6
- t.attachment :attachment
7
- end
8
- end
@@ -1,87 +0,0 @@
1
- require 'spec_helper'
2
- require 'open-uri'
3
- require 'net/http'
4
- require 'support/post'
5
-
6
- describe Paperclip::Storage::Aliyun do
7
- before :all do
8
- @file = load_attachment('girl.jpg')
9
- @post = Post.create attachment: @file
10
- end
11
-
12
- after :all do
13
- @post.destroy! if @post && @post.respond_to?(:id)
14
- @file.close
15
- end
16
-
17
- describe '#flush_writes' do
18
- it 'uploads the attachment to Aliyun' do
19
- response = open(@post.attachment.url)
20
- expect(response).to be_truthy
21
- end
22
-
23
- it 'get uploaded file from Aliyun' do
24
- attachment = open @post.attachment.url
25
- expect(attachment.size).to eq(@file.size)
26
- end
27
-
28
- it 'set content type according to the original file' do
29
- attachment = load_attachment('masu.pdf')
30
- post = Post.create attachment: attachment
31
- headers = RestClient.head(post.attachment.url).headers
32
- expect(headers[:content_type]).to eq('application/pdf')
33
-
34
- post.destroy
35
- end
36
- end
37
-
38
- describe '#exists?' do
39
- it 'returns true if the file exists on Aliyun' do
40
- expect(@post.attachment).to exist
41
- end
42
-
43
- it "returns false if the file doesn't exist on Aliyun" do
44
- post = Post.new attachment: @file
45
- expect(post.attachment).not_to exist
46
- end
47
-
48
- it 'not raise exception when attachment not saved' do
49
- post = Post.create
50
- expect { post.attachment.exists? }.not_to raise_error
51
- end
52
- end
53
-
54
- describe '#copy_to_local_file' do
55
- it 'copies file from Aliyun to a local file' do
56
- destination = File.join(Bundler.root, 'tmp/photo.jpg')
57
- @post.attachment.copy_to_local_file(:original, destination)
58
- expect(File.exist?(destination)).to be_truthy
59
-
60
- File.delete destination
61
- end
62
- end
63
-
64
- describe '#flush_deletes' do
65
- it 'deletes the attachment from Aliyun' do
66
- attachment_url = @post.attachment.url
67
- @post.destroy
68
-
69
- response_code = Net::HTTP.get_response(URI.parse(attachment_url)).code
70
- expect(response_code).to eq('404')
71
- end
72
-
73
- context "work with path include Chinese characters" do
74
- before do
75
- @file_with_chinese_char_name = load_attachment("美女.jpg")
76
- @post_with_chinese_char_name_file = Post.create attachment: @file_with_chinese_char_name
77
- end
78
-
79
- it "deletes the attachment from Aliyun" do
80
- attachment_url = @post_with_chinese_char_name_file.attachment.url
81
- @post_with_chinese_char_name_file.destroy
82
- response_code = Net::HTTP.get_response(URI.parse(attachment_url)).code
83
- expect(response_code).to eq('404')
84
- end
85
- end
86
- end
87
- end
data/spec/spec_helper.rb DELETED
@@ -1,27 +0,0 @@
1
- require 'pry'
2
- require 'pry-nav'
3
- require 'paperclip-storage-aliyun'
4
-
5
- Paperclip.logger.level = ::Logger::UNKNOWN
6
- Dir[Bundler.root.join('spec/support/**/*.rb')].each(&method(:require))
7
-
8
- # Aliyun defaults
9
- OSS_CONNECTION_OPTIONS = {
10
- access_id: '4adTRa4dWto3vxiq',
11
- access_key: 'hzEWBDWlt3N0SPPj6EfYAr4ISdaizW',
12
- bucket: 'martin-test',
13
- data_center: 'hangzhou',
14
- internal: false
15
- # host_alias: nil
16
- }
17
-
18
- # Paperclip defaults
19
- Paperclip::Attachment.default_options[:storage] = :aliyun
20
- Paperclip::Attachment.default_options[:aliyun] = OSS_CONNECTION_OPTIONS
21
- Paperclip::Attachment.default_options[:path] = 'public/system/:class/:attachment/:id_partition/:style/:filename'
22
- Paperclip::Attachment.default_options[:url] = ':aliyun_upload_url'
23
-
24
- # Utility methods
25
- def load_attachment(file_name)
26
- File.open(Bundler.root.join("spec/attachments/#{file_name}"), 'rb')
27
- end
data/spec/support/post.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'active_record'
2
- require 'nulldb'
3
- require 'paperclip'
4
-
5
- class Post < ActiveRecord::Base
6
- include Paperclip::Glue
7
-
8
- has_attached_file :attachment
9
- do_not_validate_attachment_file_type :attachment
10
- end
11
-
12
- RSpec.configure do |config|
13
- config.before(:all) do
14
- FileUtils.mkdir_p File.join(Bundler.root, 'tmp')
15
- ActiveRecord::Base.establish_connection(
16
- adapter: 'nulldb',
17
- schema: File.join(Bundler.root, 'spec/fixtures/schema.rb')
18
- )
19
- end
20
-
21
- config.after(:all) do
22
- ActiveRecord::Base.remove_connection
23
- end
24
- end
@@ -1,10 +0,0 @@
1
- require 'paperclip'
2
-
3
- module Paperclip
4
- module Interpolations
5
- module Rails
6
- def self.root
7
- end
8
- end
9
- end
10
- end