carrierwave-qiniu 0.2.6 → 1.0.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: e4f2676e8b7b98df69a4d64794ff9aa123fc6129
4
- data.tar.gz: 0f2e39106d8f4ec702cc497535b1ecebe9f38be2
3
+ metadata.gz: 9eebff093b52ff063969297675687f497ccadcda
4
+ data.tar.gz: ee4b5d2023a718c8e25fce76fe5c79de8b21c43f
5
5
  SHA512:
6
- metadata.gz: 3b37ff5997e8456fa4893d1fa080582685f4a56ece2efaff66e1f426b067306c4a6fd7b6449083351ada005a3096459e6ef7d30d9c09c2d01889c2e650b2c611
7
- data.tar.gz: cf8851827113cfd0379c43da1dd3c084421f785ed4de64c78a151ac3c191994d694d75305c3d9b7335ddfadc626f9f86d5990f592ac19ad281e4cd9319b8529e
6
+ metadata.gz: 0d2b6d7dc64fb5d401131444a09cb7d56840d2909b6d9be31a706c5e8b1932ad9e1544095b688e89902df7dfe2ef63d5e645ba3c49bd88fceb155ccf2e66e908
7
+ data.tar.gz: c7e8a550038550afe3d953b87bf51d921ed77eee8cb728c66d7091a0b3d1a9fdab814282b0bb5f135035d68df186a169eeb2b843628cddbb5fdc4fea3496d597
data/CHANGELOG.md CHANGED
@@ -1,10 +1,21 @@
1
1
 
2
2
  ## CHANGE LOG
3
3
 
4
+ ### v1.0.0
5
+
6
+ - 变更图片样式的用法
7
+ https://github.com/huobazi/carrierwave-qiniu/pull/70
8
+
9
+ - 不兼容上一版本
10
+
11
+ ``` qiniu_styles ``` 移动到 config 中
12
+ 在 uploader 内 使用 ``` use_qiniu_styles ``` 来指定使用默认 styles 或者 覆盖默认配置
13
+ 详见 ReadMe 中的示例
14
+
4
15
  ### v0.2.6
5
16
 
6
17
  - 提供图片样式的便利方法
7
- https://github.com/huobazi/qiniu_direct_uploader/pull/68
18
+ https://github.com/huobazi/carrierwave-qiniu/pull/68
8
19
 
9
20
 
10
21
  ### v0.2.5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Carrierwave::Qiniu
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/carrierwave-qiniu@2x.png?0.2.6)](http://badge.fury.io/rb/carrierwave-qiniu)
3
+ [![Gem Version](https://badge.fury.io/rb/carrierwave-qiniu@2x.png?1.0.0)](http://badge.fury.io/rb/carrierwave-qiniu)
4
4
 
5
5
  This gem adds storage support for [Qiniu](http://qiniutek.com) to [Carrierwave](https://github.com/jnicklas/carrierwave)
6
6
  example: https://github.com/huobazi/carrierwave-qiniu-example
@@ -9,7 +9,7 @@ example: https://github.com/huobazi/carrierwave-qiniu-example
9
9
 
10
10
  Add this line to your application's Gemfile:
11
11
 
12
- gem 'carrierwave-qiniu'
12
+ gem 'carrierwave-qiniu', '~> 1.0.0'
13
13
 
14
14
  And then execute:
15
15
 
@@ -80,12 +80,17 @@ end
80
80
  ```
81
81
 
82
82
  You can use [qiniu image styles](https://qiniu.kf5.com/hc/kb/article/68884/) instead [version](https://github.com/carrierwaveuploader/carrierwave#adding-versions) processing of CarrierWave.
83
- ```
84
- # Case 1
83
+
84
+ ```ruby
85
+ # Case 1: Array styles
86
+ CarrierWave.configure do |config|
87
+ config.qiniu_styles = [:thumb, :large]
88
+ end
89
+
85
90
  class AvatarUploader < CarrierWave::Uploader::Base
86
91
  storage :qiniu
87
92
 
88
- qiniu_styles [:thumb, :large]
93
+ use_qiniu_styles
89
94
  end
90
95
 
91
96
  # original url
@@ -96,11 +101,9 @@ user.avatar.url(:thumb)
96
101
  # http://.../avatar.jpg-thumb
97
102
 
98
103
 
99
- # Case 2
100
- class AvatarUploader < CarrierWave::Uploader::Base
101
- storage :qiniu
102
-
103
- qiniu_styles thumb: 'imageView2/1/w/200', large: 'imageView2/1/w/800'
104
+ # Case 2: Hash styles
105
+ CarrierWave.configure do |config|
106
+ config.qiniu_styles = { thumb: 'imageView2/1/w/200', large: 'imageView2/1/w/800' }
104
107
  end
105
108
 
106
109
  # thumb url
@@ -114,8 +117,47 @@ user.avatar.url(:thumb, inline: true)
114
117
  # just style param
115
118
  user.avatar.url(style: 'imageView2/1/w/200')
116
119
  # http://.../avatar.jpg?imageView2/1/w/200
120
+
121
+ # Case 3: Inline all styles in development environment
122
+ CarrierWave.configure do |config|
123
+ config.qiniu_styles = { thumb: 'imageView2/1/w/200', large: 'imageView2/1/w/800' }
124
+ config.qiniu_style_inline = true if Rails.env.development?
125
+ end
126
+
127
+ class AvatarUploader < CarrierWave::Uploader::Base
128
+ storage :qiniu
129
+
130
+ use_qiniu_styles
131
+ end
132
+
133
+ user.avatar.url(:thumb)
134
+ # http://.../avatar.jpg?imageView2/1/w/200
135
+
136
+ # Case 4: Custom styles and bucket
137
+ class AvatarUploader < CarrierWave::Uploader::Base
138
+ storage :qiniu
139
+
140
+ # Override default styles and use your own
141
+ use_qiniu_styles :thumb => 'imageView/0/w/400', :xlarge => 'imageView/0/w/1600'
142
+
143
+ self.qiniu_bucket = "avatars"
144
+ self.qiniu_bucket_domain = "avatars.files.example.com"
145
+
146
+ end
147
+
148
+ user.avatar.url(:thumb, inline: true)
149
+ # http://.../avatar.jpg?imageView2/1/w/400
150
+
117
151
  ```
152
+ Sync Qiniu styles of uploader
118
153
 
154
+ ```
155
+ $ rake carrierwave:qiniu:sync_styles
156
+
157
+ # Bucket: bucket_name_1, Set style: thumb => imageView2/1/w/200
158
+ # Bucket: bucket_name_2, Set style: large => imageView2/1/w/800
159
+
160
+ ```
119
161
  You can see a example project on: https://github.com/huobazi/carrierwave-qiniu-example
120
162
 
121
163
  or see the spec test on https://github.com/huobazi/carrierwave-qiniu/blob/master/spec/upload_spec.rb
@@ -17,6 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.version = Carrierwave::Qiniu::VERSION
18
18
 
19
19
 
20
- gem.add_dependency "carrierwave"
21
- gem.add_dependency "qiniu",["~> 6.8.0"]
20
+ gem.add_dependency "carrierwave" , "~> 0"
21
+ gem.add_dependency "qiniu", "~> 6.8", ">= 6.8.0"
22
22
  end
@@ -4,6 +4,7 @@ require "carrierwave/storage/qiniu"
4
4
  require "carrierwave/qiniu/configuration"
5
5
  require "carrierwave/qiniu/style"
6
6
  require "carrierwave/uploader/base"
7
+ require "carrierwave/qiniu/railtie" if defined?(Rails)
7
8
 
8
9
  ::CarrierWave.configure do |config|
9
10
  config.storage_engines[:qiniu] = "::CarrierWave::Storage::Qiniu".freeze
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Carrierwave
3
3
  module Qiniu
4
- VERSION = "0.2.6"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
@@ -21,6 +21,8 @@ module CarrierWave
21
21
  add_config :qiniu_up_host
22
22
  add_config :qiniu_private_url_expires_in
23
23
  add_config :qiniu_style_separator
24
+ add_config :qiniu_style_inline
25
+ add_config :qiniu_styles
24
26
 
25
27
  alias_config :qiniu_protocal, :qiniu_protocol
26
28
 
@@ -41,6 +43,7 @@ module CarrierWave
41
43
  config.qiniu_callback_body = ''
42
44
  config.qiniu_persistent_notify_url = ''
43
45
  config.qiniu_style_separator = '-'
46
+ config.qiniu_style_inline = false
44
47
  end
45
48
  end
46
49
 
@@ -0,0 +1,10 @@
1
+ module CarrierWave
2
+ module Qiniu
3
+
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'carrierwave/tasks/qiniu.rake'
7
+ end
8
+ end
9
+ end
10
+ end
@@ -8,26 +8,48 @@ module CarrierWave
8
8
  class_methods do
9
9
  # === Examples:
10
10
  #
11
- # qiniu_styles [:thumbnail, :large]
12
- # qiniu_styles :thumbnail => 'imageView/0/w/200', :large => 'imageView/0/w/800'
13
- # qiniu_styles
11
+ # CarrierWave.configure do |config|
12
+ # config.qiniu_styles = [:thumbnail, :large]
13
+ # # or
14
+ # config.qiniu_styles = {:thumbnail => 'imageView/0/w/200', :large => 'imageView/0/w/800'}
15
+ # end
14
16
  #
15
- def qiniu_styles(versions = nil)
17
+ # # Eanble qiniu styles otherwise default version processing
18
+ # # And use default styles
19
+ # use_qiniu_styles
20
+ #
21
+ # # Override default styles and use your own styles
22
+ # use_qniu_styles :thumbnail => 'imageView/0/w/400', :xlarge => 'imageView/0/w/1600'
23
+ #
24
+ def use_qiniu_styles(versions = nil)
25
+
16
26
  # Override #url method when set styles, otherwise still default strategy.
17
27
  unless include? ::CarrierWave::Qiniu::Url
18
28
  send(:include, ::CarrierWave::Qiniu::Url)
19
29
  end
20
30
 
21
- @qiniu_styles = {}
22
- if versions.is_a? Array
23
- @qiniu_styles = versions.map { |version| [version.to_sym, nil] }.to_h
24
- elsif versions.is_a? Hash
25
- @qiniu_styles = versions.symbolize_keys
31
+ @_qiniu_styles = {}
32
+ if self.qiniu_styles
33
+ # Set default styles
34
+ @_qiniu_styles = parse_qiniu_styles(self.qiniu_styles)
35
+ elsif versions
36
+ # Set custom styles
37
+ self.qiniu_styles = versions
38
+ @_qiniu_styles = parse_qiniu_styles(versions)
26
39
  end
27
40
  end
28
41
 
29
42
  def get_qiniu_styles
30
- @qiniu_styles
43
+ @_qiniu_styles
44
+ end
45
+
46
+ private
47
+ def parse_qiniu_styles(styles)
48
+ if styles.is_a? Array
49
+ styles.map { |version| [version.to_sym, nil] }.to_h
50
+ elsif styles.is_a? Hash
51
+ styles.symbolize_keys
52
+ end
31
53
  end
32
54
  end
33
55
  end
@@ -10,25 +10,31 @@ module CarrierWave
10
10
  def url(*args)
11
11
  return super if args.empty?
12
12
 
13
+ # Usage: avatar.url(style: 'imageView/0/w/200')
13
14
  if args.first.is_a? Hash
14
15
  options = args.first
15
- # Usage: avatar.url(style: 'imageView/0/w/200')
16
16
  if options[:style]
17
17
  url = super({})
18
18
  return "#{url}?#{options[:style]}"
19
19
  end
20
20
  else
21
+ # Usage: avatar.url(version, options)
21
22
  version = args.first.to_sym
22
- if qiniu_styles.key? version.to_sym
23
+ if styles.key? version.to_sym
23
24
  options = args.last
24
25
 
25
26
  # TODO: handle private url
26
27
  url = super({})
27
28
  # Usage: avatar.url(:version, inline: true)
28
- if options.present? && options.is_a?(Hash) && options[:inline] && qiniu_styles[version]
29
- return "#{url}?#{qiniu_styles[version]}"
29
+ if options.present? && options.is_a?(Hash) && options[:inline] && styles[version]
30
+ return "#{url}?#{styles[version]}"
30
31
  else # Usage: avatar.url(:version)
31
- return "#{url}#{self.class.qiniu_style_separator}#{version}"
32
+ # inline mode
33
+ if self.class.qiniu_style_inline && styles[version]
34
+ return "#{url}?#{styles[version]}"
35
+ else
36
+ return "#{url}#{self.class.qiniu_style_separator}#{version}"
37
+ end
32
38
  end
33
39
  end
34
40
  end
@@ -37,7 +43,7 @@ module CarrierWave
37
43
  super
38
44
  end
39
45
 
40
- def qiniu_styles
46
+ def styles
41
47
  self.class.get_qiniu_styles
42
48
  end
43
49
  end
@@ -0,0 +1,21 @@
1
+ namespace :carrierwave do
2
+ namespace :qiniu do
3
+ desc 'Sync Qiniu styles of uploader'
4
+ task sync_styles: :environment do
5
+ options = [:qiniu_access_key, :qiniu_secret_key, :qiniu_block_size, :qiniu_up_host].reduce({}) do |options, key|
6
+ options.merge!(key => CarrierWave::Uploader::Base.public_send(key))
7
+ end
8
+ # Config Qiniu establish_connection
9
+ CarrierWave::Storage::Qiniu::Connection.new(options)
10
+
11
+ bucket = CarrierWave::Uploader::Base.qiniu_bucket
12
+ styles = CarrierWave::Uploader::Base.qiniu_styles
13
+ if styles && styles.is_a?(Hash)
14
+ styles.each do |name, style|
15
+ puts "Bucket: #{bucket}, Set style: #{name} => #{style}"
16
+ Qiniu.set_style(bucket, name.to_s, style)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
data/spec/upload_spec.rb CHANGED
@@ -155,53 +155,62 @@ require 'carrierwave/processing/mini_magick'
155
155
  end
156
156
 
157
157
  context "Styles" do
158
+ class StylesUploader < CarrierWave::Uploader::Base
159
+ use_qiniu_styles
160
+ end
158
161
 
159
- context 'array styles' do
160
- class ArrayStylesUploader < CarrierWave::Uploader::Base
161
- qiniu_styles [:thumb]
162
+ class StyledPhoto < ActiveRecord::Base
163
+ self.table_name = 'photos'
162
164
 
163
- def store_dir
164
- "carrierwave-qiniu-spec"
165
- end
166
- end
165
+ mount_uploader :image, StylesUploader
166
+ end
167
167
 
168
- class ArrayStyledPhoto < ActiveRecord::Base
169
- self.table_name = 'photos'
168
+ class CustomStylesUploader < CarrierWave::Uploader::Base
169
+ use_qiniu_styles thumb2: 'imageView2/0/w/200'
170
+ end
170
171
 
171
- mount_uploader :image, ArrayStylesUploader
172
- end
172
+ class CustomStyledPhoto < ActiveRecord::Base
173
+ self.table_name = 'photos'
174
+
175
+ mount_uploader :image, CustomStylesUploader
176
+ end
177
+
178
+ let(:photo) {
179
+ f = load_file("mm.jpg")
180
+ photo = StyledPhoto.new(image: f)
181
+ photo.save
182
+ photo
183
+ }
173
184
 
185
+ context 'array styles' do
174
186
  it 'style url' do
175
- f = load_file("mm.jpg")
176
- photo = ArrayStyledPhoto.new(image: f)
177
- photo.save
187
+ StylesUploader.qiniu_styles = [:thumb]
188
+ StylesUploader.use_qiniu_styles
189
+
178
190
  photo.errors.count.should == 0
179
191
 
180
192
  expect(photo.image.url).not_to be_nil
181
193
  puts photo.image.url('thumb')
182
194
  expect(photo.image.url('thumb').end_with?("mm.jpg-thumb")).to eq true
183
195
  end
184
- end
185
196
 
186
- context "Hash styles" do
187
- class HashStylesUploader < CarrierWave::Uploader::Base
188
- qiniu_styles thumb: "imageView2/0/w/200"
197
+ it 'global inline mode' do
198
+ StylesUploader.qiniu_styles = [:thumb]
199
+ StylesUploader.use_qiniu_styles
200
+ CarrierWave.configure {|config| config.qiniu_style_inline = true }
189
201
 
190
- def store_dir
191
- "carrierwave-qiniu-spec"
192
- end
202
+ expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg-thumb")).to eq true
203
+ CarrierWave.configure {|config| config.qiniu_style_inline = false }
193
204
  end
205
+ end
194
206
 
195
- class HashStyledPhoto < ActiveRecord::Base
196
- self.table_name = 'photos'
197
-
198
- mount_uploader :image, HashStylesUploader
207
+ context "Hash styles" do
208
+ before :each do
209
+ StylesUploader.qiniu_styles = { thumb: 'imageView2/0/w/200' }
210
+ StylesUploader.use_qiniu_styles
199
211
  end
200
212
 
201
213
  it 'style url' do
202
- f = load_file("mm.jpg")
203
- photo = HashStyledPhoto.new(image: f)
204
- photo.save
205
214
  photo.errors.count.should == 0
206
215
 
207
216
  expect(photo.image.url).not_to be_nil
@@ -210,33 +219,58 @@ require 'carrierwave/processing/mini_magick'
210
219
  end
211
220
 
212
221
  it 'inline style url' do
213
- f = load_file("mm.jpg")
214
- photo = HashStyledPhoto.new(image: f)
215
- photo.save
216
222
  puts photo.image.url('thumb', inline: true)
217
223
  expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
218
224
  end
225
+
226
+ it 'global inline mode' do
227
+ CarrierWave.configure {|config| config.qiniu_style_inline = true }
228
+ expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
229
+ CarrierWave.configure {|config| config.qiniu_style_inline = false }
230
+ end
219
231
  end
220
232
 
221
233
  context "Only Style param" do
222
- class StylesUploader < CarrierWave::Uploader::Base
223
- qiniu_styles
234
+ it 'url' do
235
+ puts photo.image.url(style: 'imageView2/0/w/200')
236
+ expect(photo.image.url(style: 'imageView2/0/w/200').end_with?("mm.jpg?imageView2/0/w/200")).to eq true
224
237
  end
238
+ end
225
239
 
226
- class StyledPhoto < ActiveRecord::Base
227
- self.table_name = 'photos'
240
+ context "Custom styles" do
241
+ let(:custom_photo) {
242
+ f = load_file("mm.jpg")
243
+ photo = CustomStyledPhoto.new(image: f)
244
+ photo.save
245
+ photo
246
+ }
247
+
248
+ it 'override default styles' do
249
+ photo = custom_photo
250
+ expect(CustomStylesUploader.qiniu_styles).to eq({ thumb2: 'imageView2/0/w/200' })
251
+ # Version thumb doesn't exist!
252
+ expect { photo.image.url('thumb') }.to raise_error
253
+ end
228
254
 
229
- mount_uploader :image, StylesUploader
255
+ it 'style url' do
256
+ photo = custom_photo
257
+ expect(photo.image.url).not_to be_nil
258
+ puts photo.image.url('thumb2')
259
+ expect(photo.image.url('thumb2').end_with?("mm.jpg-thumb2")).to eq true
230
260
  end
231
261
 
232
- it 'url' do
233
- f = load_file("mm.jpg")
234
- photo = StyledPhoto.new(image: f)
235
- photo.save
236
- puts photo.image.url(style: 'imageView2/0/w/200')
237
- expect(photo.image.url(style: 'imageView2/0/w/200').end_with?("mm.jpg?imageView2/0/w/200")).to eq true
262
+ it 'inline style url' do
263
+ photo = custom_photo
264
+ puts photo.image.url('thumb2', inline: true)
265
+ expect(photo.image.url('thumb2', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
238
266
  end
239
- end
240
267
 
268
+ it 'global inline mode' do
269
+ photo = custom_photo
270
+ CarrierWave.configure {|config| config.qiniu_style_inline = true }
271
+ expect(photo.image.url('thumb2', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
272
+ CarrierWave.configure {|config| config.qiniu_style_inline = false }
273
+ end
274
+ end
241
275
  end
242
276
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-qiniu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marble Wu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-17 00:00:00.000000000 Z
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
@@ -29,6 +29,9 @@ dependencies:
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.8'
34
+ - - ">="
32
35
  - !ruby/object:Gem::Version
33
36
  version: 6.8.0
34
37
  type: :runtime
@@ -36,6 +39,9 @@ dependencies:
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
41
  - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '6.8'
44
+ - - ">="
39
45
  - !ruby/object:Gem::Version
40
46
  version: 6.8.0
41
47
  description: Qiniu Storage support for CarrierWave
@@ -56,9 +62,11 @@ files:
56
62
  - lib/carrierwave-qiniu.rb
57
63
  - lib/carrierwave-qiniu/version.rb
58
64
  - lib/carrierwave/qiniu/configuration.rb
65
+ - lib/carrierwave/qiniu/railtie.rb
59
66
  - lib/carrierwave/qiniu/style.rb
60
67
  - lib/carrierwave/qiniu/url.rb
61
68
  - lib/carrierwave/storage/qiniu.rb
69
+ - lib/carrierwave/tasks/qiniu.rake
62
70
  - lib/carrierwave/uploader/base.rb
63
71
  - spec/mm.jpg
64
72
  - spec/spec_helper.rb
@@ -83,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
91
  version: '0'
84
92
  requirements: []
85
93
  rubyforge_project:
86
- rubygems_version: 2.4.6
94
+ rubygems_version: 2.5.1
87
95
  signing_key:
88
96
  specification_version: 4
89
97
  summary: Qiniu Storage support for CarrierWave