carrierwave-qiniu 1.2.1 → 1.2.2

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.
@@ -0,0 +1,166 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class QiniuFile
6
+
7
+ attr_reader :uploader, :path
8
+ attr_accessor :copy_from_path
9
+
10
+ def initialize(uploader, path)
11
+ @uploader, @path = uploader, path
12
+ end
13
+
14
+ ##
15
+ # Return qiniu URl, maybe with style
16
+ #
17
+ # === Parameters
18
+ # [options (Hash)] optional options hash, 图片样式 { version: :thumb } 或者 { style: "imageView2/1/w/200" }
19
+ #
20
+ # === Returns
21
+ #
22
+ # [String]
23
+ #
24
+ def url(options = {})
25
+ the_path = options.present? ? path_with_style(options) : @path
26
+ qiniu_connection.download_url(the_path)
27
+ end
28
+
29
+ def store(new_file)
30
+ if new_file.is_a?(self.class)
31
+ if new_file.respond_to?(:copy_from_path) && new_file.copy_from_path.present?
32
+ new_file.copy_from new_file.copy_from_path
33
+ else
34
+ new_file.copy_to @path
35
+ end
36
+ else
37
+ qiniu_connection.upload_file(new_file.path, @path)
38
+ end
39
+ true
40
+ end
41
+
42
+ def delete
43
+ qiniu_connection.delete @path
44
+ end
45
+
46
+ def exists?
47
+ return true if qiniu_connection.stat(@path).present?
48
+ false
49
+ end
50
+
51
+ #
52
+ # @note 从指定路径复制文件
53
+ # @param origin_path [String] 原文件路径
54
+ # @return [Boolean]
55
+ #
56
+ def copy_from(origin_path)
57
+ qiniu_connection.copy(origin_path, @path)
58
+ end
59
+
60
+ #
61
+ # @note 复制文件到指定路径
62
+ # @param new_path [String] 新路径
63
+ # @return [Boolean]
64
+ #
65
+ def copy_to(new_path)
66
+ qiniu_connection.copy(@path, new_path)
67
+ self.class.new(@uploader, new_path)
68
+ end
69
+
70
+ #
71
+ # @note 移动文件到指定路径
72
+ # @param new_path [String] 新路径
73
+ # @return [Boolean]
74
+ #
75
+ def move_to(new_path)
76
+ qiniu_connection.move(@path, new_path)
77
+ self.class.new(@uploader, new_path)
78
+ end
79
+
80
+ ##
81
+ # Reads the contents of the file from Cloud Files
82
+ #
83
+ # === Returns
84
+ #
85
+ # [String] contents of the file
86
+ #
87
+ def read
88
+ qiniu_connection.get(@path) if self.size > 0
89
+ end
90
+
91
+ def content_type
92
+ file_info["mimeType"] || "application/octet-stream".freeze
93
+ end
94
+
95
+ def size
96
+ file_info["fsize"] || 0
97
+ end
98
+
99
+ def extension
100
+ @path.split(".").last
101
+ end
102
+
103
+ def filename
104
+ ::File.basename(@path)
105
+ end
106
+
107
+ def original_filename
108
+ return @original_filename if @original_filename
109
+ if @file && @file.respond_to?(:original_filename)
110
+ @file.original_filename
111
+ elsif @path
112
+ ::File.basename(@path)
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ def qiniu_connection
119
+ config = {
120
+ :qiniu_access_key => @uploader.qiniu_access_key,
121
+ :qiniu_secret_key => @uploader.qiniu_secret_key,
122
+ :qiniu_bucket => @uploader.qiniu_bucket,
123
+ :qiniu_bucket_domain => @uploader.qiniu_bucket_domain,
124
+ :qiniu_bucket_private => @uploader.qiniu_bucket_private,
125
+ :qiniu_block_size => @uploader.qiniu_block_size,
126
+ :qiniu_protocol => @uploader.qiniu_protocol,
127
+ :qiniu_expires_in => @uploader.qiniu_expires_in,
128
+ :qiniu_up_host => @uploader.qiniu_up_host,
129
+ :qiniu_private_url_expires_in => @uploader.qiniu_private_url_expires_in,
130
+ :qiniu_callback_url => @uploader.qiniu_callback_url,
131
+ :qiniu_callback_body => @uploader.qiniu_callback_body,
132
+ :qiniu_persistent_notify_url => @uploader.qiniu_persistent_notify_url,
133
+ :qiniu_persistent_pipeline => @uploader.qiniu_persistent_pipeline,
134
+ :qiniu_style_separator => @uploader.qiniu_style_separator,
135
+ :qiniu_delete_after_days => @uploader.qiniu_delete_after_days,
136
+ }
137
+
138
+ if (@uploader.qiniu_persistent_ops.present? && @uploader.qiniu_persistent_ops.size > 0)
139
+ config[:qiniu_persistent_ops] = Array(@uploader.qiniu_persistent_ops).join(";")
140
+ else
141
+ # 适配老版本持久化参数 qiniu_async_ops
142
+ config[:qiniu_persistent_ops] = Array(@uploader.qiniu_async_ops).join(";") if (@uploader.respond_to?(:qiniu_async_ops) && @uploader.qiniu_async_ops.present? && @uploader.qiniu_async_ops.size > 0)
143
+ end
144
+ config[:qiniu_can_overwrite] = @uploader.try :qiniu_can_overwrite rescue false
145
+ @qiniu_connection = ::CarrierWave::Qiniu::Connection.new config
146
+ end
147
+
148
+ def file_info
149
+ @file_info ||= qiniu_connection.stat(@path)
150
+ end
151
+
152
+ def path_with_style(options)
153
+ return @path unless options
154
+ if options.has_key?(:version)
155
+ version = options[:version]
156
+ return "#{@path}#{@uploader.class.qiniu_style_separator}#{version}"
157
+ elsif options.has_key?(:style)
158
+ style = options[:style]
159
+ return "#{@path}?#{style}"
160
+ else
161
+ return @path
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
@@ -6,7 +6,7 @@ namespace :carrierwave do
6
6
  options.merge!(key => CarrierWave::Uploader::Base.public_send(key))
7
7
  end
8
8
  # Config Qiniu establish_connection
9
- CarrierWave::Storage::Qiniu::Connection.new(options)
9
+ CarrierWave::Qiniu::Connection.new(options)
10
10
 
11
11
  bucket = CarrierWave::Uploader::Base.qiniu_bucket
12
12
  styles = CarrierWave::Uploader::Base.qiniu_styles
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: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marble Wu
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-19 00:00:00.000000000 Z
11
+ date: 2020-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -28,22 +28,22 @@ dependencies:
28
28
  name: qiniu
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 6.9.0
34
31
  - - "~>"
35
32
  - !ruby/object:Gem::Version
36
33
  version: '6.9'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 6.9.0
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: 6.9.0
44
41
  - - "~>"
45
42
  - !ruby/object:Gem::Version
46
43
  version: '6.9'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 6.9.0
47
47
  description: Qiniu Storage support for CarrierWave
48
48
  email:
49
49
  - huobazi@gmail.com
@@ -51,32 +51,38 @@ executables: []
51
51
  extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
+ - ".env.sample"
54
55
  - ".gitignore"
55
56
  - ".rspec"
57
+ - ".rspec_status"
58
+ - ".ruby-version"
56
59
  - CHANGELOG.md
57
60
  - Gemfile
58
61
  - Gemfile.lock
59
- - LICENSE
62
+ - LICENSE.txt
60
63
  - README.md
61
64
  - Rakefile
62
65
  - _config.yml
66
+ - bin/console
67
+ - bin/setup
63
68
  - carrierwave-qiniu.gemspec
64
69
  - lib/carrierwave-qiniu.rb
65
- - lib/carrierwave-qiniu/version.rb
66
70
  - lib/carrierwave/qiniu/configuration.rb
71
+ - lib/carrierwave/qiniu/connection.rb
67
72
  - lib/carrierwave/qiniu/railtie.rb
68
73
  - lib/carrierwave/qiniu/style.rb
69
74
  - lib/carrierwave/qiniu/url.rb
75
+ - lib/carrierwave/qiniu/version.rb
70
76
  - lib/carrierwave/storage/qiniu.rb
77
+ - lib/carrierwave/storage/qiniu_file.rb
71
78
  - lib/carrierwave/tasks/qiniu.rake
72
- - lib/carrierwave/uploader/base.rb
73
- - spec/mm.jpg
74
- - spec/spec_helper.rb
75
- - spec/upload_spec.rb
76
79
  homepage: https://github.com/huobazi/carrierwave-qiniu
77
80
  licenses:
78
81
  - MIT
79
- metadata: {}
82
+ metadata:
83
+ homepage_uri: https://github.com/huobazi/carrierwave-qiniu
84
+ source_code_uri: https://github.com/huobazi/carrierwave-qiniu
85
+ changelog_uri: https://github.com/huobazi/carrierwave-qiniu/blob/master/CHANGELOG.md.md
80
86
  post_install_message:
81
87
  rdoc_options: []
82
88
  require_paths:
@@ -85,18 +91,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
91
  requirements:
86
92
  - - ">="
87
93
  - !ruby/object:Gem::Version
88
- version: '0'
94
+ version: 2.3.0
89
95
  required_rubygems_version: !ruby/object:Gem::Requirement
90
96
  requirements:
91
97
  - - ">="
92
98
  - !ruby/object:Gem::Version
93
99
  version: '0'
94
100
  requirements: []
95
- rubygems_version: 3.0.3
101
+ rubygems_version: 3.1.2
96
102
  signing_key:
97
103
  specification_version: 4
98
104
  summary: Qiniu Storage support for CarrierWave
99
- test_files:
100
- - spec/mm.jpg
101
- - spec/spec_helper.rb
102
- - spec/upload_spec.rb
105
+ test_files: []
data/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2012 Marble Wu
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,21 +0,0 @@
1
- #encoding: utf-8
2
- module CarrierWave
3
- class SanitizedFile
4
- attr_accessor :copy_from_path
5
- end
6
-
7
- module Uploader
8
- module Cache
9
- alias_method :old_cache!, :cache!
10
- def cache!(new_file = sanitized_file)
11
- old_cache! new_file
12
- if new_file.kind_of? CarrierWave::Storage::Qiniu::File
13
- @file.copy_from_path = new_file.path
14
- elsif new_file.kind_of? CarrierWave::Uploader::Base
15
- return unless new_file.file.present?
16
- @file.copy_from_path = new_file.file.path if @file.respond_to?(:copy_from_path)
17
- end
18
- end
19
- end
20
- end
21
- end
Binary file
@@ -1,53 +0,0 @@
1
- # encoding: utf-8
2
- require "rubygems"
3
- require "rails"
4
- require "active_record"
5
- require "carrierwave"
6
- require "carrierwave/orm/activerecord"
7
- require "dotenv"
8
- require "mini_magick"
9
-
10
- $LOAD_PATH.unshift(File.dirname(__FILE__))
11
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
12
-
13
- require "carrierwave-qiniu"
14
-
15
- module Rails
16
- class << self
17
- def root
18
- [File.expand_path(__FILE__).split("/")[0..-3].join("/"), "spec"].join("/")
19
- end
20
- end
21
- end
22
-
23
- the_gem = Gem::Specification.find_by_name("carrierwave")
24
- the_gem_root = the_gem.gem_dir
25
- the_gem_lib = the_gem_root + "/lib"
26
- the_gem_locale = the_gem_lib + "/carrierwave/locale/en.yml"
27
- I18n.load_path << the_gem_locale
28
-
29
- Dotenv.load
30
-
31
- ActiveRecord::Migration.verbose = false
32
-
33
- if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [4, 2]
34
- ActiveRecord::Base.raise_in_transactional_callbacks = true
35
- end
36
-
37
- # 测试的时候载入环境变量
38
- # 或者在根目录下新建 `.env` 文件,包含 <key>=<value>
39
- ::CarrierWave.configure do |config|
40
- config.storage = :qiniu
41
- config.qiniu_access_key = ENV["qiniu_access_key"]
42
- config.qiniu_secret_key = ENV["qiniu_secret_key"]
43
-
44
- config.qiniu_bucket = ENV["qiniu_bucket"]
45
- config.qiniu_bucket_domain = ENV["qiniu_bucket_domain"]
46
- end
47
-
48
- def load_file(fname)
49
- File.open([Rails.root, fname].join("/"))
50
- end
51
-
52
- RSpec.configure do |config|
53
- end
@@ -1,274 +0,0 @@
1
- # encoding: utf-8
2
- # thanks for https://github.com/nowa/carrierwave-upyun/blob/master/spec/upload_spec.rb
3
- require File.dirname(__FILE__) + '/spec_helper'
4
- require "open-uri"
5
-
6
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
7
-
8
- describe "CarrierWave Qiniu" do
9
- def setup_db
10
- ActiveRecord::Schema.define(:version => 1) do
11
- create_table :photos do |t|
12
- t.column :image, :string
13
- end
14
- end
15
- end
16
-
17
- def drop_db
18
- ActiveRecord::Base.connection.tables.each do |table|
19
- ActiveRecord::Base.connection.drop_table(table)
20
- end
21
- end
22
-
23
- require 'carrierwave/processing/mini_magick'
24
- class PhotoUploader < CarrierWave::Uploader::Base
25
- include CarrierWave::MiniMagick
26
-
27
- self.qiniu_can_overwrite = true
28
-
29
- version :thumb do
30
- process :resize_to_fill => [200, 200]
31
- end
32
-
33
- def store_dir
34
- "carrierwave-qiniu-spec"
35
- end
36
-
37
- def filename
38
- "images/#{secure_token(10)}.#{file.extension}" if original_filename.present?
39
- end
40
-
41
- # See
42
- # https://github.com/qiniu/ruby-sdk/issues/48
43
- # http://docs.qiniu.com/api/put.html#uploadToken
44
- # http://docs.qiniutek.com/v3/api/io/#uploadToken-asyncOps
45
- def qiniu_async_ops
46
- commands = []
47
- %W(small little middle large).each do |style|
48
- commands << "http://#{self.qiniu_bucket_domain}/#{self.store_dir}/#{self.filename}/#{style}"
49
- end
50
- commands
51
- end
52
-
53
-
54
- protected
55
- def secure_token(length = 16)
56
- var = :"@#{mounted_as}_secure_token"
57
- model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
58
- end
59
- end
60
-
61
- class Photo < ActiveRecord::Base
62
- mount_uploader :image, PhotoUploader
63
- end
64
-
65
- before :all do
66
- setup_db
67
- end
68
-
69
- after :all do
70
- drop_db
71
- end
72
-
73
- context "Upload Image" do
74
- it "should save failed" do
75
- class WrongUploader < PhotoUploader
76
- self.qiniu_bucket = 'not_exists'
77
- end
78
-
79
- class WrongPhoto < ActiveRecord::Base
80
- self.table_name = 'photos'
81
-
82
- mount_uploader :image, WrongUploader
83
- end
84
-
85
- f = load_file("mm.jpg")
86
- photo = WrongPhoto.new(:image => f)
87
- expect {
88
- photo.save!
89
- }.to raise_error
90
- end
91
-
92
- it "does upload image" do
93
- f = load_file("mm.jpg")
94
- photo = Photo.new(:image => f)
95
- photo.save
96
-
97
- puts photo.errors.full_messages if photo.errors.count > 0
98
-
99
- photo.errors.count.should == 0
100
-
101
- puts 'The image was uploaded to:'
102
- puts photo.image.url
103
-
104
- URI.open(photo.image.url).should_not be_nil
105
-
106
- puts "The thumb image:"
107
- puts photo.image.url(:thumb)
108
-
109
- URI.open(photo.image.thumb.url).should_not be_nil
110
-
111
- end
112
-
113
- it 'does copy from image works' do
114
- f = load_file("mm.jpg")
115
-
116
- photo = Photo.new(image: f)
117
-
118
- photo.save
119
-
120
- photo2 = Photo.new
121
-
122
- photo2.image = photo.image
123
-
124
- photo2.save
125
-
126
- puts "The image was copied from #{photo.image.url} to #{photo2.image.url}"
127
-
128
- expect(photo2.image.url).not_to eq(photo.image.url)
129
-
130
- URI.open(photo2.image.url).should_not be_nil
131
- end
132
-
133
- describe 'after remove' do
134
- before(:each) do
135
- f = load_file("mm.jpg")
136
- @photo = Photo.new(image: f)
137
- @photo.save
138
- @photo.image.remove!
139
- end
140
-
141
- it 'will be not cached' do
142
- expect(@photo.image).not_to be_cached
143
- end
144
-
145
- it 'file will be nil' do
146
- expect(@photo.image.file).to be_nil
147
- end
148
-
149
- it 'url will be nil' do
150
- expect(@photo.image.url).to be_nil
151
- end
152
- end
153
- end
154
-
155
- context "Styles" do
156
- class StylesUploader < CarrierWave::Uploader::Base
157
- use_qiniu_styles
158
- end
159
-
160
- class StyledPhoto < ActiveRecord::Base
161
- self.table_name = 'photos'
162
-
163
- mount_uploader :image, StylesUploader
164
- end
165
-
166
- class CustomStylesUploader < CarrierWave::Uploader::Base
167
- use_qiniu_styles thumb2: 'imageView2/0/w/200'
168
- end
169
-
170
- class CustomStyledPhoto < ActiveRecord::Base
171
- self.table_name = 'photos'
172
-
173
- mount_uploader :image, CustomStylesUploader
174
- end
175
-
176
- let(:photo) {
177
- f = load_file("mm.jpg")
178
- photo = StyledPhoto.new(image: f)
179
- photo.save
180
- photo
181
- }
182
-
183
- context 'array styles' do
184
- it 'style url' do
185
- StylesUploader.qiniu_styles = [:thumb]
186
- StylesUploader.use_qiniu_styles
187
-
188
- photo.errors.count.should == 0
189
-
190
- expect(photo.image.url).not_to be_nil
191
- puts photo.image.url('thumb')
192
- expect(photo.image.url('thumb').end_with?("mm.jpg-thumb")).to eq true
193
- end
194
-
195
- it 'global inline mode' do
196
- StylesUploader.qiniu_styles = [:thumb]
197
- StylesUploader.use_qiniu_styles
198
- CarrierWave.configure {|config| config.qiniu_style_inline = true }
199
-
200
- expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg-thumb")).to eq true
201
- CarrierWave.configure {|config| config.qiniu_style_inline = false }
202
- end
203
- end
204
-
205
- context "Hash styles" do
206
- before :each do
207
- StylesUploader.qiniu_styles = { thumb: 'imageView2/0/w/200' }
208
- StylesUploader.use_qiniu_styles
209
- end
210
-
211
- it 'style url' do
212
- photo.errors.count.should == 0
213
-
214
- expect(photo.image.url).not_to be_nil
215
- puts photo.image.url('thumb')
216
- expect(photo.image.url('thumb').end_with?("mm.jpg-thumb")).to eq true
217
- end
218
-
219
- it 'inline style url' do
220
- puts photo.image.url('thumb', inline: true)
221
- expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
222
- end
223
-
224
- it 'global inline mode' do
225
- CarrierWave.configure {|config| config.qiniu_style_inline = true }
226
- expect(photo.image.url('thumb', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
227
- CarrierWave.configure {|config| config.qiniu_style_inline = false }
228
- end
229
- end
230
-
231
- context "Only Style param" do
232
- it 'url' do
233
- puts photo.image.url(style: 'imageView2/0/w/200')
234
- expect(photo.image.url(style: 'imageView2/0/w/200').end_with?("mm.jpg?imageView2/0/w/200")).to eq true
235
- end
236
- end
237
-
238
- context "Custom styles" do
239
- let(:custom_photo) {
240
- f = load_file("mm.jpg")
241
- photo = CustomStyledPhoto.new(image: f)
242
- photo.save
243
- photo
244
- }
245
-
246
- it 'override default styles' do
247
- photo = custom_photo
248
- expect(CustomStylesUploader.qiniu_styles).to eq({ thumb2: 'imageView2/0/w/200' })
249
- # Version thumb doesn't exist!
250
- expect { photo.image.url('thumb') }.to raise_error
251
- end
252
-
253
- it 'style url' do
254
- photo = custom_photo
255
- expect(photo.image.url).not_to be_nil
256
- puts photo.image.url('thumb2')
257
- expect(photo.image.url('thumb2').end_with?("mm.jpg-thumb2")).to eq true
258
- end
259
-
260
- it 'inline style url' do
261
- photo = custom_photo
262
- puts photo.image.url('thumb2', inline: true)
263
- expect(photo.image.url('thumb2', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
264
- end
265
-
266
- it 'global inline mode' do
267
- photo = custom_photo
268
- CarrierWave.configure {|config| config.qiniu_style_inline = true }
269
- expect(photo.image.url('thumb2', inline: true).end_with?("mm.jpg?imageView2/0/w/200")).to eq true
270
- CarrierWave.configure {|config| config.qiniu_style_inline = false }
271
- end
272
- end
273
- end
274
- end