carrierwave-qiniu-new 0.1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +16 -0
- data/LICENSE +22 -0
- data/README.md +95 -0
- data/Rakefile +2 -0
- data/carrierwave-qiniu.gemspec +22 -0
- data/lib/carrierwave-qiniu.rb +10 -0
- data/lib/carrierwave-qiniu/version.rb +6 -0
- data/lib/carrierwave/qiniu/configuration.rb +43 -0
- data/lib/carrierwave/storage/qiniu.rb +177 -0
- data/spec/mm.jpg +0 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/upload_spec.rb +85 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37e130f8606067f2e420f849230bc805efc8399b
|
4
|
+
data.tar.gz: 67aeaa8fff6a7df2a4714d4c70d38baeb9c165ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9295fe2f840f8ffb1a1cf403c9815cdfb2580b5ed103115b4b21c69b0c61b233f7cfc4ae9455d4d7ef12d49a67fa71da8ade2b363874918d6065dbbc9201a602
|
7
|
+
data.tar.gz: d76917b30e83885ce0c0725bdcba7c8233c56799fc4e9a59f843cc175a96a2ad534c1a29e14fda3ecd43a273733ef58566fbdda79044eec23d1d9513fad12f67
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
## CHANGE LOG
|
3
|
+
|
4
|
+
### v0.1.8.1
|
5
|
+
|
6
|
+
https://github.com/huobazi/carrierwave-qiniu/pull/36
|
7
|
+
|
8
|
+
https://github.com/huobazi/carrierwave-qiniu/pull/38
|
9
|
+
|
10
|
+
### v0.1.7
|
11
|
+
|
12
|
+
- 添加从云端读取(下载)文件 。 [https://github.com/huobazi/carrierwave-qiniu/pull/27](https://github.com/huobazi/carrierwave-qiniu/pull/27)
|
13
|
+
|
14
|
+
- 增加获取有时效性url链接地址的方法 。 [https://github.com/huobazi/carrierwave-qiniu/issues/25](https://github.com/huobazi/carrierwave-qiniu/issues/25)
|
15
|
+
|
16
|
+
- 升级七牛 SDK 至 6.4.2
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#source 'https://rubygems.org'
|
2
|
+
source 'http://ruby.taobao.org'
|
3
|
+
|
4
|
+
# Specify your gem's dependencies in carrierwave-qiniu.gemspec
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'rails', '>=3.2.8'
|
9
|
+
gem 'sqlite3', '>=1.3.6'
|
10
|
+
gem 'carrierwave', '>=0.6.2'
|
11
|
+
gem 'mini_magick', '>=3.4'
|
12
|
+
gem 'qiniu'
|
13
|
+
gem 'rspec', '~> 2.11'
|
14
|
+
gem 'mocha', '>=0.10.0'
|
15
|
+
gem 'dotenv'
|
16
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Carrierwave::Qiniu
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/carrierwave-qiniu@2x.png?20150410001)](http://badge.fury.io/rb/carrierwave-qiniu)
|
4
|
+
|
5
|
+
This gem adds storage support for [Qiniu](http://qiniutek.com) to [Carrierwave](https://github.com/jnicklas/carrierwave)
|
6
|
+
example: https://github.com/huobazi/carrierwave-qiniu-example
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'carrierwave-qiniu'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install carrierwave-qiniu
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
You'll need to configure it in config/initializes/carrierwave.rb
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
::CarrierWave.configure do |config|
|
28
|
+
config.storage = :qiniu
|
29
|
+
config.qiniu_access_key = "your qiniu access_key"
|
30
|
+
config.qiniu_secret_key = 'your qiniu secret_key'
|
31
|
+
config.qiniu_bucket = "carrierwave-qiniu-example"
|
32
|
+
config.qiniu_bucket_domain = "carrierwave-qiniu-example.aspxboy.com"
|
33
|
+
config.qiniu_bucket_private= true #default is false
|
34
|
+
config.qiniu_block_size = 4*1024*1024
|
35
|
+
config.qiniu_protocol = "http"
|
36
|
+
|
37
|
+
config.qiniu_up_host = 'http://up.qiniug.com' #七牛上传海外服务器,国内使用可以不要这行配置
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
For more information on qiniu, please read http://developer.qiniu.com/docs/v6/
|
42
|
+
|
43
|
+
And then in your uploader, set the storage to `:qiniu`:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
47
|
+
storage :qiniu
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
You can override configuration item in individual uploader like this:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
55
|
+
storage :qiniu
|
56
|
+
|
57
|
+
self.qiniu_bucket = "avatars"
|
58
|
+
self.qiniu_bucket_domain = "avatars.files.example.com"
|
59
|
+
self.qiniu_protocal = 'http'
|
60
|
+
self.qiniu_can_overwrite = true
|
61
|
+
self.qiniu_bucket_private= true #default is false
|
62
|
+
|
63
|
+
# 指定预转数据处理命令
|
64
|
+
# https://github.com/qiniu/ruby-sdk/issues/48
|
65
|
+
# http://docs.qiniu.com/api/put.html#uploadToken
|
66
|
+
# http://docs.qiniutek.com/v3/api/io/#uploadToken-asyncOps
|
67
|
+
# http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html#put-policy-persistent-ops-explanation
|
68
|
+
def qiniu_async_ops
|
69
|
+
commands = []
|
70
|
+
%W(small little middle large).each do |style|
|
71
|
+
commands << "http://#{self.qiniu_bucket_domain}/#{self.store_dir}/#{self.filename}/#{style}"
|
72
|
+
end
|
73
|
+
commands
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
```
|
78
|
+
You can see a example project on: https://github.com/huobazi/carrierwave-qiniu-example
|
79
|
+
or see the spec test on https://github.com/huobazi/carrierwave-qiniu/blob/master/spec/upload_spec.rb
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create new Pull Request
|
88
|
+
|
89
|
+
## Contributors
|
90
|
+
|
91
|
+
See the [Contributors List](https://github.com/huobazi/carrierwave-qiniu/graphs/contributors).
|
92
|
+
|
93
|
+
## CHANGE LOG
|
94
|
+
|
95
|
+
See the [CHANGELOGS.md](https://github.com/huobazi/carrierwave-qiniu/blob/master/CHANGELOG.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/carrierwave-qiniu/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Marble Wu"]
|
6
|
+
gem.email = ["huobazi@gmail.com"]
|
7
|
+
gem.description = %q{Qiniu Storage support for CarrierWave}
|
8
|
+
gem.summary = %q{Qiniu Storage support for CarrierWave}
|
9
|
+
gem.homepage = "https://github.com/huobazi/carrierwave-qiniu"
|
10
|
+
gem.license = "MIT"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "carrierwave-qiniu-new"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = Carrierwave::Qiniu::VERSION
|
18
|
+
|
19
|
+
|
20
|
+
gem.add_dependency "carrierwave", ['0.10.0']
|
21
|
+
gem.add_dependency "qiniu",["~> 6.4"]
|
22
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "carrierwave/storage/qiniu"
|
3
|
+
require "carrierwave/qiniu/configuration"
|
4
|
+
require "carrierwave-qiniu/version"
|
5
|
+
|
6
|
+
::CarrierWave.configure do |config|
|
7
|
+
config.storage_engines[:qiniu] = "::CarrierWave::Storage::Qiniu".freeze
|
8
|
+
end
|
9
|
+
|
10
|
+
::CarrierWave::Uploader::Base.send(:include, ::CarrierWave::Qiniu::Configuration)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Qiniu
|
5
|
+
module Configuration
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
included do
|
8
|
+
add_config :qiniu_bucket_domain
|
9
|
+
add_config :qiniu_bucket
|
10
|
+
add_config :qiniu_bucket_private
|
11
|
+
add_config :qiniu_access_key
|
12
|
+
add_config :qiniu_secret_key
|
13
|
+
add_config :qiniu_block_size
|
14
|
+
add_config :qiniu_protocol
|
15
|
+
add_config :qiniu_async_ops
|
16
|
+
add_config :qiniu_can_overwrite
|
17
|
+
add_config :qiniu_expires_in
|
18
|
+
add_config :qiniu_up_host
|
19
|
+
add_config :qiniu_private_url_expires_in
|
20
|
+
|
21
|
+
alias_config :qiniu_protocal, :qiniu_protocol
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def alias_config(new_name, old_name)
|
26
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
27
|
+
def self.#{new_name}(value=nil)
|
28
|
+
self.#{old_name}(value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.#{new_name}=(value)
|
32
|
+
self.#{old_name}=(value)
|
33
|
+
end
|
34
|
+
|
35
|
+
def #{new_name}
|
36
|
+
#{old_name}
|
37
|
+
end
|
38
|
+
RUBY
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'carrierwave'
|
3
|
+
require 'qiniu'
|
4
|
+
require 'qiniu/http'
|
5
|
+
|
6
|
+
module CarrierWave
|
7
|
+
module Storage
|
8
|
+
class Qiniu < Abstract
|
9
|
+
|
10
|
+
class Connection
|
11
|
+
def initialize(options={})
|
12
|
+
@qiniu_bucket_domain = options[:qiniu_bucket_domain]
|
13
|
+
@qiniu_bucket = options[:qiniu_bucket]
|
14
|
+
@qiniu_bucket_private= options[:qiniu_bucket_private] || false
|
15
|
+
@qiniu_access_key = options[:qiniu_access_key]
|
16
|
+
@qiniu_secret_key = options[:qiniu_secret_key]
|
17
|
+
@qiniu_block_size = options[:qiniu_block_size] || 1024*1024*4
|
18
|
+
@qiniu_protocol = options[:qiniu_protocol] || "http"
|
19
|
+
@qiniu_async_ops = options[:qiniu_async_ops] || ''
|
20
|
+
@qiniu_can_overwrite = options[:qiniu_can_overwrite] || false
|
21
|
+
@qiniu_expires_in = options[:qiniu_expires_in] || options[:expires_in] || 3600
|
22
|
+
@qiniu_up_host = options[:qiniu_up_host]
|
23
|
+
@qiniu_private_url_expires_in = options[:qiniu_private_url_expires_in] || 3600
|
24
|
+
init
|
25
|
+
end
|
26
|
+
|
27
|
+
def store(file, key)
|
28
|
+
overwrite_file = nil
|
29
|
+
overwrite_file = key if @qiniu_can_overwrite
|
30
|
+
|
31
|
+
put_policy = ::Qiniu::Auth::PutPolicy.new(
|
32
|
+
@qiniu_bucket,
|
33
|
+
overwrite_file,
|
34
|
+
@qiniu_expires_in,
|
35
|
+
nil
|
36
|
+
)
|
37
|
+
put_policy.persistent_ops = @qiniu_async_ops
|
38
|
+
|
39
|
+
::Qiniu::Storage.upload_with_put_policy(
|
40
|
+
put_policy,
|
41
|
+
file.path,
|
42
|
+
key
|
43
|
+
)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
def delete(key)
|
48
|
+
::Qiniu::Storage.delete(@qiniu_bucket, key) rescue nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def stat(key)
|
52
|
+
code, result, _ = ::Qiniu::Storage.stat(@qiniu_bucket, key)
|
53
|
+
code == 200 ? result : {}
|
54
|
+
end
|
55
|
+
|
56
|
+
def get(path)
|
57
|
+
code, result, _ = ::Qiniu::HTTP.get( download_url(path) )
|
58
|
+
code == 200 ? result : nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def download_url(path)
|
62
|
+
encode_path = URI.escape(path) #fix chinese file name, same as encodeURIComponent in js but preserve slash '/'
|
63
|
+
primitive_url = "#{@qiniu_protocol}://#{@qiniu_bucket_domain}/#{encode_path}"
|
64
|
+
@qiniu_bucket_private ? \
|
65
|
+
::Qiniu::Auth.authorize_download_url(primitive_url, :expires_in => @qiniu_private_url_expires_in) \
|
66
|
+
: \
|
67
|
+
primitive_url
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def init
|
74
|
+
init_qiniu_rs_connection
|
75
|
+
end
|
76
|
+
|
77
|
+
UserAgent = "CarrierWave-Qiniu/#{Carrierwave::Qiniu::VERSION} (#{RUBY_PLATFORM}) Ruby/#{RUBY_VERSION}".freeze
|
78
|
+
|
79
|
+
def init_qiniu_rs_connection
|
80
|
+
options = {
|
81
|
+
:access_key => @qiniu_access_key,
|
82
|
+
:secret_key => @qiniu_secret_key,
|
83
|
+
:user_agent => UserAgent
|
84
|
+
}
|
85
|
+
options[:block_size] = @qiniu_block_size if @qiniu_block_size
|
86
|
+
options[:up_host] = @qiniu_up_host if @qiniu_up_host
|
87
|
+
|
88
|
+
::Qiniu.establish_connection! options
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
class File
|
95
|
+
|
96
|
+
def initialize(uploader, path)
|
97
|
+
@uploader, @path = uploader, path
|
98
|
+
end
|
99
|
+
|
100
|
+
def path
|
101
|
+
@path
|
102
|
+
end
|
103
|
+
|
104
|
+
def url
|
105
|
+
qiniu_connection.download_url(@path)
|
106
|
+
end
|
107
|
+
|
108
|
+
def store(file)
|
109
|
+
qiniu_connection.store(file, @path)
|
110
|
+
end
|
111
|
+
|
112
|
+
def delete
|
113
|
+
qiniu_connection.delete(@path)
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
# Reads the contents of the file from Cloud Files
|
118
|
+
#
|
119
|
+
# === Returns
|
120
|
+
#
|
121
|
+
# [String] contents of the file
|
122
|
+
#
|
123
|
+
def read
|
124
|
+
qiniu_connection.get(@path) if self.size > 0
|
125
|
+
end
|
126
|
+
|
127
|
+
def content_type
|
128
|
+
file_info['mimeType'] || 'application/octet-stream'.freeze
|
129
|
+
end
|
130
|
+
|
131
|
+
def size
|
132
|
+
file_info['fsize'] || 0
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def qiniu_connection
|
138
|
+
@qiniu_connection ||= begin
|
139
|
+
config = {
|
140
|
+
:qiniu_access_key => @uploader.qiniu_access_key,
|
141
|
+
:qiniu_secret_key => @uploader.qiniu_secret_key,
|
142
|
+
:qiniu_bucket => @uploader.qiniu_bucket,
|
143
|
+
:qiniu_bucket_domain => @uploader.qiniu_bucket_domain,
|
144
|
+
:qiniu_bucket_private=> @uploader.qiniu_bucket_private,
|
145
|
+
:qiniu_block_size => @uploader.qiniu_block_size,
|
146
|
+
:qiniu_protocol => @uploader.qiniu_protocol,
|
147
|
+
:qiniu_expires_in => @uploader.qiniu_expires_in,
|
148
|
+
:qiniu_up_host => @uploader.qiniu_up_host,
|
149
|
+
:qiniu_private_url_expires_in => @uploader.qiniu_private_url_expires_in
|
150
|
+
}
|
151
|
+
|
152
|
+
config[:qiniu_async_ops] = Array(@uploader.qiniu_async_ops).join(';') rescue ''
|
153
|
+
config[:qiniu_can_overwrite] = @uploader.try :qiniu_can_overwrite rescue false
|
154
|
+
|
155
|
+
Connection.new config
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def file_info
|
160
|
+
@file_info ||= qiniu_connection.stat(@path)
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
def store!(file)
|
166
|
+
f = ::CarrierWave::Storage::Qiniu::File.new(uploader, uploader.store_path(uploader.filename))
|
167
|
+
f.store(file)
|
168
|
+
f
|
169
|
+
end
|
170
|
+
|
171
|
+
def retrieve!(identifier)
|
172
|
+
::CarrierWave::Storage::Qiniu::File.new(uploader, uploader.store_path(identifier))
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
data/spec/mm.jpg
ADDED
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "rubygems"
|
3
|
+
require "rspec"
|
4
|
+
require "rspec/autorun"
|
5
|
+
require "rails"
|
6
|
+
require "active_record"
|
7
|
+
require "carrierwave"
|
8
|
+
require "carrierwave/orm/activerecord"
|
9
|
+
require 'dotenv'
|
10
|
+
|
11
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"..","lib"))
|
13
|
+
|
14
|
+
require "carrierwave-qiniu"
|
15
|
+
|
16
|
+
module Rails
|
17
|
+
class <<self
|
18
|
+
def root
|
19
|
+
[File.expand_path(__FILE__).split('/')[0..-3].join('/'),"spec"].join("/")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Dotenv.load
|
25
|
+
|
26
|
+
ActiveRecord::Migration.verbose = false
|
27
|
+
|
28
|
+
# 测试的时候载入环境变量
|
29
|
+
# 或者在根目录下新建 `.env` 文件,包含 <key>=<value>
|
30
|
+
::CarrierWave.configure do |config|
|
31
|
+
config.storage = :qiniu
|
32
|
+
config.qiniu_access_key = ENV['qiniu_access_key']
|
33
|
+
config.qiniu_secret_key = ENV['qiniu_secret_key']
|
34
|
+
|
35
|
+
config.qiniu_bucket = ENV['qiniu_bucket']
|
36
|
+
config.qiniu_bucket_domain = ENV['qiniu_bucket_domain']
|
37
|
+
|
38
|
+
config.qiniu_block_size = 4*1024*1024
|
39
|
+
config.qiniu_protocol = "http"
|
40
|
+
end
|
41
|
+
|
42
|
+
def load_file(fname)
|
43
|
+
File.open([Rails.root,fname].join("/"))
|
44
|
+
end
|
data/spec/upload_spec.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# thanks for https://github.com/nowa/carrierwave-upyun/blob/master/spec/upload_spec.rb
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
5
|
+
require "open-uri"
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
8
|
+
|
9
|
+
describe "CarrierWave Qiniu" do
|
10
|
+
def setup_db
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
create_table :photos do |t|
|
13
|
+
t.column :image, :string
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def drop_db
|
19
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
20
|
+
ActiveRecord::Base.connection.drop_table(table)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class PhotoUploader < CarrierWave::Uploader::Base
|
25
|
+
|
26
|
+
def store_dir
|
27
|
+
"carrierwave-qiniu-spec"
|
28
|
+
end
|
29
|
+
|
30
|
+
def filename
|
31
|
+
"images/#{secure_token(10)}.#{file.extension}" if original_filename.present?
|
32
|
+
end
|
33
|
+
|
34
|
+
# See
|
35
|
+
# https://github.com/qiniu/ruby-sdk/issues/48
|
36
|
+
# http://docs.qiniu.com/api/put.html#uploadToken
|
37
|
+
# http://docs.qiniutek.com/v3/api/io/#uploadToken-asyncOps
|
38
|
+
def qiniu_async_ops
|
39
|
+
commands = []
|
40
|
+
%W(small little middle large).each do |style|
|
41
|
+
commands << "http://#{self.qiniu_bucket_domain}/#{self.store_dir}/#{self.filename}/#{style}"
|
42
|
+
end
|
43
|
+
commands
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
protected
|
48
|
+
def secure_token(length = 16)
|
49
|
+
var = :"@#{mounted_as}_secure_token"
|
50
|
+
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Photo < ActiveRecord::Base
|
55
|
+
|
56
|
+
mount_uploader :image, PhotoUploader
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
before :all do
|
61
|
+
setup_db
|
62
|
+
end
|
63
|
+
|
64
|
+
after :all do
|
65
|
+
drop_db
|
66
|
+
end
|
67
|
+
|
68
|
+
context "Upload Image" do
|
69
|
+
it "does upload image" do
|
70
|
+
f = load_file("mm.jpg")
|
71
|
+
photo = Photo.new(:image => f)
|
72
|
+
photo.save
|
73
|
+
|
74
|
+
photo.errors.count.should == 0
|
75
|
+
|
76
|
+
puts ""
|
77
|
+
puts 'The image was uploaded to:'
|
78
|
+
puts photo.image.url
|
79
|
+
|
80
|
+
open(photo.image.url).should_not be_nil
|
81
|
+
|
82
|
+
puts photo.image.url
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-qiniu-new
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.8.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marble Wu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: carrierwave
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.10.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: qiniu
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.4'
|
41
|
+
description: Qiniu Storage support for CarrierWave
|
42
|
+
email:
|
43
|
+
- huobazi@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CHANGELOG.md
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- carrierwave-qiniu.gemspec
|
55
|
+
- lib/carrierwave-qiniu.rb
|
56
|
+
- lib/carrierwave-qiniu/version.rb
|
57
|
+
- lib/carrierwave/qiniu/configuration.rb
|
58
|
+
- lib/carrierwave/storage/qiniu.rb
|
59
|
+
- spec/mm.jpg
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- spec/upload_spec.rb
|
62
|
+
homepage: https://github.com/huobazi/carrierwave-qiniu
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.2.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Qiniu Storage support for CarrierWave
|
86
|
+
test_files:
|
87
|
+
- spec/mm.jpg
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/upload_spec.rb
|