carrierwave-qiniu 0.2.5 → 0.2.6
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +39 -1
- data/lib/carrierwave-qiniu.rb +2 -0
- data/lib/carrierwave-qiniu/version.rb +1 -1
- data/lib/carrierwave/qiniu/configuration.rb +19 -0
- data/lib/carrierwave/qiniu/style.rb +35 -0
- data/lib/carrierwave/qiniu/url.rb +45 -0
- data/lib/carrierwave/storage/qiniu.rb +3 -1
- data/spec/spec_helper.rb +0 -4
- data/spec/upload_spec.rb +91 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4f2676e8b7b98df69a4d64794ff9aa123fc6129
|
4
|
+
data.tar.gz: 0f2e39106d8f4ec702cc497535b1ecebe9f38be2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b37ff5997e8456fa4893d1fa080582685f4a56ece2efaff66e1f426b067306c4a6fd7b6449083351ada005a3096459e6ef7d30d9c09c2d01889c2e650b2c611
|
7
|
+
data.tar.gz: cf8851827113cfd0379c43da1dd3c084421f785ed4de64c78a151ac3c191994d694d75305c3d9b7335ddfadc626f9f86d5990f592ac19ad281e4cd9319b8529e
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
|
2
2
|
## CHANGE LOG
|
3
3
|
|
4
|
+
### v0.2.6
|
5
|
+
|
6
|
+
- 提供图片样式的便利方法
|
7
|
+
https://github.com/huobazi/qiniu_direct_uploader/pull/68
|
8
|
+
|
9
|
+
|
4
10
|
### v0.2.5
|
5
11
|
|
6
12
|
https://github.com/huobazi/carrierwave-qiniu/pull/61
|
13
|
+
|
7
14
|
https://github.com/huobazi/carrierwave-qiniu/pull/62
|
8
15
|
|
9
16
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Carrierwave::Qiniu
|
2
2
|
|
3
|
-
[](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
|
@@ -78,6 +78,44 @@ class AvatarUploader < CarrierWave::Uploader::Base
|
|
78
78
|
|
79
79
|
end
|
80
80
|
```
|
81
|
+
|
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
|
85
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
86
|
+
storage :qiniu
|
87
|
+
|
88
|
+
qiniu_styles [:thumb, :large]
|
89
|
+
end
|
90
|
+
|
91
|
+
# original url
|
92
|
+
user.avatar.url
|
93
|
+
|
94
|
+
# thumb url
|
95
|
+
user.avatar.url(:thumb)
|
96
|
+
# http://.../avatar.jpg-thumb
|
97
|
+
|
98
|
+
|
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
|
+
end
|
105
|
+
|
106
|
+
# thumb url
|
107
|
+
user.avatar.url(:thumb)
|
108
|
+
# http://.../avatar.jpg-thumb
|
109
|
+
|
110
|
+
# inline thubm url
|
111
|
+
user.avatar.url(:thumb, inline: true)
|
112
|
+
# http://.../avatar.jpg?imageView2/1/w/200
|
113
|
+
|
114
|
+
# just style param
|
115
|
+
user.avatar.url(style: 'imageView2/1/w/200')
|
116
|
+
# http://.../avatar.jpg?imageView2/1/w/200
|
117
|
+
```
|
118
|
+
|
81
119
|
You can see a example project on: https://github.com/huobazi/carrierwave-qiniu-example
|
82
120
|
|
83
121
|
or see the spec test on https://github.com/huobazi/carrierwave-qiniu/blob/master/spec/upload_spec.rb
|
data/lib/carrierwave-qiniu.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
require "carrierwave-qiniu/version"
|
3
3
|
require "carrierwave/storage/qiniu"
|
4
4
|
require "carrierwave/qiniu/configuration"
|
5
|
+
require "carrierwave/qiniu/style"
|
5
6
|
require "carrierwave/uploader/base"
|
6
7
|
|
7
8
|
::CarrierWave.configure do |config|
|
@@ -9,3 +10,4 @@ require "carrierwave/uploader/base"
|
|
9
10
|
end
|
10
11
|
|
11
12
|
::CarrierWave::Uploader::Base.send(:include, ::CarrierWave::Qiniu::Configuration)
|
13
|
+
::CarrierWave::Uploader::Base.send(:include, ::CarrierWave::Qiniu::Style)
|
@@ -20,11 +20,30 @@ module CarrierWave
|
|
20
20
|
add_config :qiniu_expires_in
|
21
21
|
add_config :qiniu_up_host
|
22
22
|
add_config :qiniu_private_url_expires_in
|
23
|
+
add_config :qiniu_style_separator
|
23
24
|
|
24
25
|
alias_config :qiniu_protocal, :qiniu_protocol
|
26
|
+
|
27
|
+
reset_qiniu_config
|
25
28
|
end
|
26
29
|
|
27
30
|
module ClassMethods
|
31
|
+
# Set default value
|
32
|
+
def reset_qiniu_config
|
33
|
+
configure do |config|
|
34
|
+
config.qiniu_protocol = 'http'
|
35
|
+
config.qiniu_bucket_private = false
|
36
|
+
config.qiniu_block_size = 1024*1024*4
|
37
|
+
config.qiniu_async_ops = ''
|
38
|
+
config.qiniu_can_overwrite = false
|
39
|
+
config.qiniu_private_url_expires_in = 3600
|
40
|
+
config.qiniu_callback_url = ''
|
41
|
+
config.qiniu_callback_body = ''
|
42
|
+
config.qiniu_persistent_notify_url = ''
|
43
|
+
config.qiniu_style_separator = '-'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
28
47
|
def alias_config(new_name, old_name)
|
29
48
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
30
49
|
def self.#{new_name}(value=nil)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'carrierwave/qiniu/url'
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Qiniu
|
5
|
+
module Style
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
# === Examples:
|
10
|
+
#
|
11
|
+
# qiniu_styles [:thumbnail, :large]
|
12
|
+
# qiniu_styles :thumbnail => 'imageView/0/w/200', :large => 'imageView/0/w/800'
|
13
|
+
# qiniu_styles
|
14
|
+
#
|
15
|
+
def qiniu_styles(versions = nil)
|
16
|
+
# Override #url method when set styles, otherwise still default strategy.
|
17
|
+
unless include? ::CarrierWave::Qiniu::Url
|
18
|
+
send(:include, ::CarrierWave::Qiniu::Url)
|
19
|
+
end
|
20
|
+
|
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
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_qiniu_styles
|
30
|
+
@qiniu_styles
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Qiniu
|
3
|
+
module Url
|
4
|
+
# === Examples:
|
5
|
+
#
|
6
|
+
# avatar.url(:version)
|
7
|
+
# avatar.url(:version, inline: true)
|
8
|
+
# avatar.url(style: 'imageView2/0/w/200')
|
9
|
+
#
|
10
|
+
def url(*args)
|
11
|
+
return super if args.empty?
|
12
|
+
|
13
|
+
if args.first.is_a? Hash
|
14
|
+
options = args.first
|
15
|
+
# Usage: avatar.url(style: 'imageView/0/w/200')
|
16
|
+
if options[:style]
|
17
|
+
url = super({})
|
18
|
+
return "#{url}?#{options[:style]}"
|
19
|
+
end
|
20
|
+
else
|
21
|
+
version = args.first.to_sym
|
22
|
+
if qiniu_styles.key? version.to_sym
|
23
|
+
options = args.last
|
24
|
+
|
25
|
+
# TODO: handle private url
|
26
|
+
url = super({})
|
27
|
+
# 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]}"
|
30
|
+
else # Usage: avatar.url(:version)
|
31
|
+
return "#{url}#{self.class.qiniu_style_separator}#{version}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Fallback to original url
|
37
|
+
super
|
38
|
+
end
|
39
|
+
|
40
|
+
def qiniu_styles
|
41
|
+
self.class.get_qiniu_styles
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -24,6 +24,7 @@ module CarrierWave
|
|
24
24
|
@qiniu_callback_url = options[:qiniu_callback_url] || ''
|
25
25
|
@qiniu_callback_body = options[:qiniu_callback_body] || ''
|
26
26
|
@qiniu_persistent_notify_url = options[:qiniu_persistent_notify_url] || ''
|
27
|
+
@qiniu_style_separator = options[:qiniu_style_separator] || '-'
|
27
28
|
init
|
28
29
|
end
|
29
30
|
|
@@ -197,7 +198,8 @@ module CarrierWave
|
|
197
198
|
:qiniu_private_url_expires_in => @uploader.qiniu_private_url_expires_in,
|
198
199
|
:qiniu_callback_url => @uploader.qiniu_callback_url,
|
199
200
|
:qiniu_callback_body => @uploader.qiniu_callback_body,
|
200
|
-
:qiniu_persistent_notify_url => @uploader.qiniu_persistent_notify_url
|
201
|
+
:qiniu_persistent_notify_url => @uploader.qiniu_persistent_notify_url,
|
202
|
+
:qiniu_style_separator => @uploader.qiniu_style_separator
|
201
203
|
}
|
202
204
|
|
203
205
|
config[:qiniu_async_ops] = Array(@uploader.qiniu_async_ops).join(';') rescue ''
|
data/spec/spec_helper.rb
CHANGED
@@ -47,9 +47,6 @@ end
|
|
47
47
|
|
48
48
|
config.qiniu_bucket = ENV['qiniu_bucket']
|
49
49
|
config.qiniu_bucket_domain = ENV['qiniu_bucket_domain']
|
50
|
-
|
51
|
-
config.qiniu_block_size = 4*1024*1024
|
52
|
-
config.qiniu_protocol = "http"
|
53
50
|
end
|
54
51
|
|
55
52
|
def load_file(fname)
|
@@ -60,4 +57,3 @@ end
|
|
60
57
|
RSpec.configure do |config|
|
61
58
|
|
62
59
|
end
|
63
|
-
|
data/spec/upload_spec.rb
CHANGED
@@ -77,12 +77,15 @@ require 'carrierwave/processing/mini_magick'
|
|
77
77
|
self.qiniu_bucket = 'not_exists'
|
78
78
|
end
|
79
79
|
|
80
|
-
class
|
80
|
+
class WrongPhoto < ActiveRecord::Base
|
81
|
+
self.table_name = 'photos'
|
82
|
+
|
81
83
|
mount_uploader :image, WrongUploader
|
82
84
|
end
|
83
85
|
|
84
86
|
f = load_file("mm.jpg")
|
85
|
-
photo =
|
87
|
+
photo = WrongPhoto.new(:image => f)
|
88
|
+
# FIXME: also raise error, see: CarrierWave::Qiniu#store L54
|
86
89
|
photo.save
|
87
90
|
expect(photo).to_not be_valid
|
88
91
|
|
@@ -150,4 +153,90 @@ require 'carrierwave/processing/mini_magick'
|
|
150
153
|
expect(photo.image.file.exists?).to eq(false)
|
151
154
|
end
|
152
155
|
end
|
156
|
+
|
157
|
+
context "Styles" do
|
158
|
+
|
159
|
+
context 'array styles' do
|
160
|
+
class ArrayStylesUploader < CarrierWave::Uploader::Base
|
161
|
+
qiniu_styles [:thumb]
|
162
|
+
|
163
|
+
def store_dir
|
164
|
+
"carrierwave-qiniu-spec"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
class ArrayStyledPhoto < ActiveRecord::Base
|
169
|
+
self.table_name = 'photos'
|
170
|
+
|
171
|
+
mount_uploader :image, ArrayStylesUploader
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'style url' do
|
175
|
+
f = load_file("mm.jpg")
|
176
|
+
photo = ArrayStyledPhoto.new(image: f)
|
177
|
+
photo.save
|
178
|
+
photo.errors.count.should == 0
|
179
|
+
|
180
|
+
expect(photo.image.url).not_to be_nil
|
181
|
+
puts photo.image.url('thumb')
|
182
|
+
expect(photo.image.url('thumb').end_with?("mm.jpg-thumb")).to eq true
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "Hash styles" do
|
187
|
+
class HashStylesUploader < CarrierWave::Uploader::Base
|
188
|
+
qiniu_styles thumb: "imageView2/0/w/200"
|
189
|
+
|
190
|
+
def store_dir
|
191
|
+
"carrierwave-qiniu-spec"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
class HashStyledPhoto < ActiveRecord::Base
|
196
|
+
self.table_name = 'photos'
|
197
|
+
|
198
|
+
mount_uploader :image, HashStylesUploader
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'style url' do
|
202
|
+
f = load_file("mm.jpg")
|
203
|
+
photo = HashStyledPhoto.new(image: f)
|
204
|
+
photo.save
|
205
|
+
photo.errors.count.should == 0
|
206
|
+
|
207
|
+
expect(photo.image.url).not_to be_nil
|
208
|
+
puts photo.image.url('thumb')
|
209
|
+
expect(photo.image.url('thumb').end_with?("mm.jpg-thumb")).to eq true
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'inline style url' do
|
213
|
+
f = load_file("mm.jpg")
|
214
|
+
photo = HashStyledPhoto.new(image: f)
|
215
|
+
photo.save
|
216
|
+
puts photo.image.url('thumb', inline: true)
|
217
|
+
expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "Only Style param" do
|
222
|
+
class StylesUploader < CarrierWave::Uploader::Base
|
223
|
+
qiniu_styles
|
224
|
+
end
|
225
|
+
|
226
|
+
class StyledPhoto < ActiveRecord::Base
|
227
|
+
self.table_name = 'photos'
|
228
|
+
|
229
|
+
mount_uploader :image, StylesUploader
|
230
|
+
end
|
231
|
+
|
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
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
153
242
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-qiniu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
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-
|
11
|
+
date: 2016-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -56,6 +56,8 @@ files:
|
|
56
56
|
- lib/carrierwave-qiniu.rb
|
57
57
|
- lib/carrierwave-qiniu/version.rb
|
58
58
|
- lib/carrierwave/qiniu/configuration.rb
|
59
|
+
- lib/carrierwave/qiniu/style.rb
|
60
|
+
- lib/carrierwave/qiniu/url.rb
|
59
61
|
- lib/carrierwave/storage/qiniu.rb
|
60
62
|
- lib/carrierwave/uploader/base.rb
|
61
63
|
- spec/mm.jpg
|