carrierwave-aliyun 0.4.4 → 0.5.0

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
2
  SHA1:
3
- metadata.gz: 32069f77772a0096a898c9edb0449f2626623911
4
- data.tar.gz: 24e3c0c5cda7292b9083443b9c907887b370c01f
3
+ metadata.gz: 387db2e8862191a027f5d4dd99d1df9bdfd4b77b
4
+ data.tar.gz: 69404451e625d3703a0f4918bce885e2ad2519c2
5
5
  SHA512:
6
- metadata.gz: 3073b74446c7f29d3e8cd9973f85235f46e3df074a6028d97f5c99bc1ea8672d87589ad0af9232dd4c134511639e0b8a9d02ad6bc2b889cd9b35296d9a12bac2
7
- data.tar.gz: dc8d9f2809f437090939a91344b8588d281c513a6910e7b0f52954254ec23d60ef97aa6f995cb2b4acfba1ef03deaeb23681916b92380020a75554825777b243
6
+ metadata.gz: ee77d72ac9f0f7c3c09181cf2a745bd151d4d899c6215efb95944203aa7123d8adaf40932a7ffd8364c335c6cb76ee19b0c49b55b849e7043c4f8cc9e5edfc56
7
+ data.tar.gz: b6ac51e377646fe0f63a7217654f564192fc47507f75ff9569d6bba532eb88b676711c23766b701911d1df9af4a1c687ca3735e86f641c1148182eb1190aa5cd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.5.0
2
+
3
+ - 增加 Aliyun OSS 图片处理参数的支持,允许 url 传入 `:thumb` 以生成缩略图 URL;
4
+ - 配置项增加 `config.aliyun_img_host`。
5
+
1
6
  ## 0.4.4
2
7
 
3
8
  - 修正对 Carrierwave master 版本的支持,它们[移除了](https://github.com/carrierwaveuploader/carrierwave/pull/1813) `carrierwave/processing/mime_types`;
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://gems.ruby-china.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- carrierwave-aliyun (0.4.4)
4
+ carrierwave-aliyun (0.5.0)
5
5
  aliyun-oss-sdk (>= 0.1.6)
6
6
  carrierwave (>= 0.5.7)
7
7
 
8
8
  GEM
9
- remote: http://gems.ruby-china.org/
9
+ remote: https://rubygems.org/
10
10
  specs:
11
11
  actionmailer (4.2.0)
12
12
  actionpack (= 4.2.0)
@@ -107,7 +107,7 @@ GEM
107
107
  activesupport (= 4.2.0)
108
108
  rake (>= 0.8.7)
109
109
  thor (>= 0.18.1, < 2.0)
110
- rake (10.5.0)
110
+ rake (11.2.2)
111
111
  rspec (3.4.0)
112
112
  rspec-core (~> 3.4.0)
113
113
  rspec-expectations (~> 3.4.0)
@@ -148,4 +148,4 @@ DEPENDENCIES
148
148
  sqlite3-ruby
149
149
 
150
150
  BUNDLED WITH
151
- 1.11.2
151
+ 1.12.5
data/README.md CHANGED
@@ -28,13 +28,30 @@ CarrierWave.configure do |config|
28
28
  # 配置存储的地区数据中心,默认: cn-hangzhou
29
29
  # config.aliyun_area = "cn-hangzhou"
30
30
  # 使用自定义域名,设定此项,carrierwave 返回的 URL 将会用自定义域名
31
- # 自定于域名请 CNAME 到 you_bucket_name.oss.aliyuncs.com (you_bucket_name 是你的 bucket 的名称)
31
+ # 自定于域名请 CNAME 到 you_bucket_name.oss-cn-hangzhou.aliyuncs.com (you_bucket_name 是你的 bucket 的名称)
32
32
  config.aliyun_host = "http://foo.bar.com"
33
+ # 配置缩略图 Host,默认 #{aliyun_bucket}.img-#{aliyun_area}.aliyuncs.com
34
+ # config.aliyun_img_host = "http://you_bucket_name.img-cn-hangzhou.aliyuncs.com"
33
35
  # Bucket 为私有读取请设置 true,默认 false,以便得到的 URL 是能带有 private 空间访问权限的逻辑
34
36
  # config.aliyun_private_read = false
35
37
  end
36
38
  ```
37
39
 
38
- ## 跳过 CarrierWave 直接调用 Aliyun API
40
+ ## 阿里云 OSS 图片缩略图
39
41
 
40
- 如果你有需求想跳过 CarrierWave,直接调用 Aliyun 的接口,可以参看 `spec/aliyun_spec.rb` 里面有例子。
42
+ **0.5.0** 版本开始,carrierwave-aliyun 支持 Aliyun OSS 的图片缩略图了,你只需要在 Uploader 对象的 `url` 函数后面跟上 `:thumb` 附带缩略图参数就可以了。
43
+
44
+ > NOTE: 此方法同样支持 Private 的 Bucket 哦!
45
+
46
+ 关于阿里云 OSS 图片缩略图的详细文档,请仔细阅读: [Aliyun OSS 接入图片服务](https://help.aliyun.com/document_detail/32210.html)
47
+
48
+ ```rb
49
+ irb> User.last.avatar.url(thumb: '@100w_1c')
50
+ https://simple.img-cn-hangzhou.aliyuncs.com/users/avatar/12.png@100w_1c
51
+ irb> User.last.avatar.url(thumb: '@100w_200h_1c.jpg')
52
+ https://simple.img-cn-hangzhou.aliyuncs.com/users/avatar/12.png@100w_200h_1c.jpg
53
+ irb> User.last.avatar.url(thumb: '@100w_200h_1c_95q')
54
+ https://simple.img-cn-hangzhou.aliyuncs.com/users/avatar/12.png@100w_200h_1c_95q
55
+ # 你也可以用自定义的缩略图格式
56
+ irb> User.last.avatar.url(thumb: '@!large')
57
+ ```
@@ -10,6 +10,7 @@ module CarrierWave
10
10
  add_config :aliyun_area
11
11
  add_config :aliyun_internal
12
12
  add_config :aliyun_host
13
+ add_config :aliyun_img_host
13
14
  add_config :aliyun_private_read
14
15
  end
15
16
  end
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Aliyun
3
- VERSION = '0.4.4'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -18,6 +18,7 @@ module CarrierWave
18
18
 
19
19
  # Host for get request
20
20
  @aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
21
+ @aliyun_img_host = uploader.aliyun_img_host || "http://#{@aliyun_bucket}.img-#{@aliyun_area}.aliyuncs.com"
21
22
 
22
23
  unless @aliyun_host.include?('//')
23
24
  fail "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}"
@@ -80,15 +81,25 @@ module CarrierWave
80
81
 
81
82
  ##
82
83
  # 根据配置返回完整的上传文件的访问地址
83
- def path_to_url(path)
84
- [@aliyun_host, path].join('/')
84
+ def path_to_url(path, opts = {})
85
+ if opts[:thumb]
86
+ thumb_path = [path, opts[:thumb]].join('')
87
+ [@aliyun_img_host, thumb_path].join('/')
88
+ else
89
+ [@aliyun_host, path].join('/')
90
+ end
85
91
  end
86
92
 
87
93
  # 私有空间访问地址,会带上实时算出的 token 信息
88
94
  # 有效期 3600s
89
- def private_get_url(path)
95
+ def private_get_url(path, opts = {})
90
96
  path.sub!(PATH_PREFIX, '')
91
- oss_client.bucket_get_object_share_link(path, 3600)
97
+ if opts[:thumb]
98
+ thumb_path = [path, opts[:thumb]].join('')
99
+ img_client.bucket_get_object_share_link(thumb_path, 3600)
100
+ else
101
+ oss_client.bucket_get_object_share_link(path, 3600)
102
+ end
92
103
  end
93
104
 
94
105
  private
@@ -102,6 +113,15 @@ module CarrierWave
102
113
  @oss_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
103
114
  end
104
115
 
116
+ def img_client
117
+ return @img_client if defined?(@img_client)
118
+ opts = {
119
+ host: "img-#{@aliyun_area}.aliyuncs.com",
120
+ bucket: @aliyun_bucket
121
+ }
122
+ @img_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
123
+ end
124
+
105
125
  def oss_upload_client
106
126
  return @oss_upload_client if defined?(@oss_upload_client)
107
127
 
@@ -158,11 +178,16 @@ module CarrierWave
158
178
  nil
159
179
  end
160
180
 
161
- def url
181
+ ##
182
+ # Generate file url
183
+ # params
184
+ # :thumb - Aliyun OSS Image Processor option, etc: @100w_200h_95q
185
+ #
186
+ def url(opts = {})
162
187
  if @uploader.aliyun_private_read
163
- oss_connection.private_get_url(@path)
188
+ oss_connection.private_get_url(@path, opts)
164
189
  else
165
- oss_connection.path_to_url(@path)
190
+ oss_connection.path_to_url(@path, opts)
166
191
  end
167
192
  end
168
193
 
data/spec/aliyun_spec.rb CHANGED
@@ -58,6 +58,11 @@ describe "Aliyun" do
58
58
  expect(url).to include(*%w(Signature Expires OSSAccessKeyId))
59
59
  expect(url).to include "http://#{@uploader.aliyun_bucket}.oss-cn-beijing.aliyuncs.com/bar/foo.jpg"
60
60
  end
61
+
62
+ it "should get url with :thumb" do
63
+ url = @connection.private_get_url('bar/foo.jpg', thumb: '@100w_200h_90q')
64
+ expect(url).to include "http://#{@uploader.aliyun_bucket}.img-cn-beijing.aliyuncs.com/bar/foo.jpg@100w_200h_90q"
65
+ end
61
66
  end
62
67
 
63
68
  describe 'File' do
data/spec/upload_spec.rb CHANGED
@@ -9,10 +9,12 @@ describe "Upload" do
9
9
  ActiveRecord::Schema.define(:version => 1) do
10
10
  create_table :photos do |t|
11
11
  t.column :image, :string
12
+ t.column :content_type, :string
12
13
  end
13
14
 
14
15
  create_table :attachments do |t|
15
16
  t.column :file, :string
17
+ t.column :content_type, :string
16
18
  end
17
19
  end
18
20
  end
@@ -77,14 +79,28 @@ describe "Upload" do
77
79
  it "should get uploaded file" do
78
80
  img = open(@photo.image.url)
79
81
  expect(img.size).to eq @file.size
82
+ expect(img.content_type).to eq 'image/jpeg'
80
83
  img1 = open(@photo1.image.url)
81
84
  expect(img1.size).to eq @file1.size
85
+ expect(img1.content_type).to eq 'image/gif'
82
86
  end
83
87
 
84
88
  it "sholud get small version uploaded file" do
85
89
  expect(open(@photo.image.small.url)).not_to eq nil
86
90
  expect(open(@photo1.image.small.url)).not_to eq nil
87
91
  end
92
+
93
+ it "should get Aliyun OSS thumb url with :thumb option" do
94
+ url = @photo.image.url(thumb: '@150w_140h.png')
95
+ expect(url).to include('.img-')
96
+ expect(url).to include('@150w_140h.png')
97
+ url1 = @photo.image.url(thumb: '@!150w_140h.jpg')
98
+ expect(url1).to include('.img-')
99
+ expect(url1).to include('@!150w_140h.jpg')
100
+ img1 = open(url)
101
+ expect(img1.size).not_to eq 0
102
+ expect(img1.content_type).to eq 'image/png'
103
+ end
88
104
  end
89
105
 
90
106
  context "should update zip" do
@@ -100,6 +116,7 @@ describe "Upload" do
100
116
  it "should get uploaded file" do
101
117
  attach = open(@attachment.file.url)
102
118
  expect(attach.size).to eq @file.size
119
+ expect(attach.content_type).to eq 'application/zip'
103
120
  end
104
121
 
105
122
  it "should delete old file when upload a new file again" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-aliyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-10 00:00:00.000000000 Z
11
+ date: 2016-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  version: '0'
85
85
  requirements: []
86
86
  rubyforge_project:
87
- rubygems_version: 2.5.1
87
+ rubygems_version: 2.4.8
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Aliyun OSS support for Carrierwave