carrierwave-aliyun 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/CHANGELOG.md +6 -0
- data/Gemfile +1 -1
- data/README.md +4 -11
- data/carrierwave-aliyun.gemspec +1 -1
- data/lib/carrierwave-aliyun.rb +6 -4
- data/lib/carrierwave/aliyun/configuration.rb +5 -4
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +70 -106
- data/spec/aliyun_spec.rb +35 -16
- data/spec/spec_helper.rb +7 -8
- data/spec/upload_spec.rb +29 -28
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f959d72b871dfbb6ca109a6b576c0454fc224c71
|
4
|
+
data.tar.gz: 3ba45a1b15695038844282406f7fac04a0905067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9088ffb4ab763ecd140aa18ea74e2159b24d6d10f10ff147dff824b88d1f4fb376e693f575aae238d70e4a065fec39831f9593786cdb6e99356bf20f92753603
|
7
|
+
data.tar.gz: 6cf390ea67da68871c3e2234799753ed38a130a09031d1ebce50faba5583a358a46db531dffafe8a73e490b9ece584d14f3866a2dda30b2465b7dcae37f79fca
|
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--colour
|
1
|
+
--colour
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## 0.4.0
|
2
|
+
|
3
|
+
* 采用 aliyun-oss-sdk 来作为上传后端,不再依赖 rest-client,不再内部实现上传逻辑;
|
4
|
+
* 增加 `config.aliyun_private_read` 配置项,开启以后,返回的 @user.avatar.url 将会是带 Token 和有效期的 URL,可以用于访问私有读取空间的文件;
|
5
|
+
* 去掉 `config.aliyun_upload_host` 配置项,删除了阿里内部的支持,以后请用 0.3.x 版本;
|
6
|
+
|
1
7
|
## 0.3.6
|
2
8
|
|
3
9
|
* 修正上传中文文件名无法成功的问题;
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,18 +4,11 @@ This gem adds support for [Aliyun OSS](http://oss.aliyun.com) to [CarrierWave](h
|
|
4
4
|
|
5
5
|
- [![Gem Version](https://badge.fury.io/rb/carrierwave-aliyun.png)](https://rubygems.org/gems/carrierwave-aliyun)
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
```bash
|
10
|
-
gem install carrierwave-aliyun
|
11
|
-
```
|
12
|
-
|
13
|
-
> NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用。
|
7
|
+
> NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用,如果你需要直接用 Aliyun OSS,可以尝试用 [aliyun-oss-ruby-sdk](https://github.com/aliyun-beta/aliyun-oss-ruby-sdk) 这个 Gem。
|
14
8
|
|
15
9
|
## Using Bundler
|
16
10
|
|
17
11
|
```ruby
|
18
|
-
gem 'rest-client'
|
19
12
|
gem 'carrierwave-aliyun'
|
20
13
|
```
|
21
14
|
|
@@ -30,15 +23,15 @@ CarrierWave.configure do |config|
|
|
30
23
|
config.aliyun_access_key = 'xxxxxx'
|
31
24
|
# 你需要在 Aliyum OSS 上面提前创建一个 Bucket
|
32
25
|
config.aliyun_bucket = "simple"
|
33
|
-
# 是否使用内部连接,true - 使用 Aliyun
|
26
|
+
# 是否使用内部连接,true - 使用 Aliyun 主机内部局域网的方式访问 false - 外部网络访问
|
34
27
|
config.aliyun_internal = true
|
35
28
|
# 配置存储的地区数据中心,默认: cn-hangzhou
|
36
29
|
# config.aliyun_area = "cn-hangzhou"
|
37
30
|
# 使用自定义域名,设定此项,carrierwave 返回的 URL 将会用自定义域名
|
38
31
|
# 自定于域名请 CNAME 到 you_bucket_name.oss.aliyuncs.com (you_bucket_name 是你的 bucket 的名称)
|
39
32
|
config.aliyun_host = "http://foo.bar.com"
|
40
|
-
#
|
41
|
-
# config.
|
33
|
+
# Bucket 为私有读取请设置 true,默认 false,以便得到的 URL 是能带有 private 空间访问权限的逻辑
|
34
|
+
# config.aliyun_private_read = false
|
42
35
|
end
|
43
36
|
```
|
44
37
|
|
data/carrierwave-aliyun.gemspec
CHANGED
data/lib/carrierwave-aliyun.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'carrierwave/storage/aliyun'
|
2
2
|
require 'carrierwave/processing/mime_types'
|
3
|
-
require
|
3
|
+
require 'carrierwave/aliyun/configuration'
|
4
|
+
|
4
5
|
CarrierWave.configure do |config|
|
5
|
-
config.storage_engines.merge!({:
|
6
|
+
config.storage_engines.merge!({ aliyun: 'CarrierWave::Storage::Aliyun' })
|
6
7
|
end
|
8
|
+
|
7
9
|
CarrierWave::Uploader::Base.send(:include, CarrierWave::Aliyun::Configuration)
|
8
10
|
CarrierWave::Uploader::Base.send(:include, CarrierWave::MimeTypes)
|
9
|
-
CarrierWave::Uploader::Base.send(:process, :set_content_type)
|
11
|
+
CarrierWave::Uploader::Base.send(:process, :set_content_type)
|
@@ -2,6 +2,7 @@ module CarrierWave
|
|
2
2
|
module Aliyun
|
3
3
|
module Configuration
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
|
5
6
|
included do
|
6
7
|
add_config :aliyun_access_id
|
7
8
|
add_config :aliyun_access_key
|
@@ -9,10 +10,10 @@ module CarrierWave
|
|
9
10
|
add_config :aliyun_area
|
10
11
|
add_config :aliyun_internal
|
11
12
|
add_config :aliyun_host
|
12
|
-
add_config :
|
13
|
+
add_config :aliyun_private_read
|
13
14
|
end
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
module ClassMethods
|
17
18
|
def add_config(name)
|
18
19
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
@@ -33,7 +34,7 @@ module CarrierWave
|
|
33
34
|
value.instance_of?(Proc) ? value.call : value
|
34
35
|
end
|
35
36
|
RUBY
|
36
|
-
end
|
37
|
+
end
|
37
38
|
end
|
38
39
|
end
|
39
|
-
end
|
40
|
+
end
|
@@ -1,33 +1,23 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
require 'aliyun/oss'
|
2
3
|
require 'carrierwave'
|
3
|
-
require 'digest/md5'
|
4
|
-
require 'openssl'
|
5
4
|
require 'uri'
|
6
|
-
require "rest-client"
|
7
5
|
|
8
6
|
module CarrierWave
|
9
7
|
module Storage
|
10
8
|
class Aliyun < Abstract
|
11
9
|
|
12
10
|
class Connection
|
13
|
-
def initialize(
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
|
20
|
-
# Host for upload
|
21
|
-
if @aliyun_upload_host.nil?
|
22
|
-
if options[:aliyun_internal] == true
|
23
|
-
@aliyun_upload_host = "http://#{@aliyun_bucket}.oss-#{@aliyun_area}-internal.aliyuncs.com"
|
24
|
-
else
|
25
|
-
@aliyun_upload_host = "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
26
|
-
end
|
27
|
-
end
|
11
|
+
def initialize(uploader)
|
12
|
+
@uploader = uploader
|
13
|
+
@aliyun_access_id = uploader.aliyun_access_id
|
14
|
+
@aliyun_access_key = uploader.aliyun_access_key
|
15
|
+
@aliyun_bucket = uploader.aliyun_bucket
|
16
|
+
@aliyun_area = uploader.aliyun_area || 'cn-hangzhou'
|
17
|
+
@aliyun_private_read = uploader.aliyun_private_read
|
28
18
|
|
29
19
|
# Host for get request
|
30
|
-
@aliyun_host =
|
20
|
+
@aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
31
21
|
|
32
22
|
if not @aliyun_host.include?("http")
|
33
23
|
raise "config.aliyun_host requirement include http:// or https://, but you give: #{@aliyun_host}"
|
@@ -43,27 +33,17 @@ module CarrierWave
|
|
43
33
|
# returns:
|
44
34
|
# 图片的下载地址
|
45
35
|
def put(path, file, options={})
|
46
|
-
path
|
47
|
-
|
48
|
-
|
49
|
-
content_type = options[:content_type] || "image/jpg"
|
50
|
-
date = gmtdate
|
51
|
-
url = path_to_url(path)
|
52
|
-
|
53
|
-
host = URI.parse(url).host
|
54
|
-
|
55
|
-
auth_sign = sign("PUT", bucket_path, content_md5, content_type,date)
|
56
|
-
headers = {
|
57
|
-
"Authorization" => auth_sign,
|
58
|
-
"Content-Type" => content_type,
|
59
|
-
"Content-Length" => file.size,
|
60
|
-
"Date" => date,
|
61
|
-
"Host" => host,
|
62
|
-
"Expect" => "100-Continue"
|
36
|
+
path.sub!(/^\//, '')
|
37
|
+
opts = {
|
38
|
+
'Content-Type' => options[:content_type] || "image/jpg"
|
63
39
|
}
|
64
40
|
|
65
|
-
|
66
|
-
|
41
|
+
res = oss_upload_client.bucket_create_object(path, file, opts)
|
42
|
+
if res.success?
|
43
|
+
path_to_url(path)
|
44
|
+
else
|
45
|
+
raise "Put file failed"
|
46
|
+
end
|
67
47
|
end
|
68
48
|
|
69
49
|
# 读取文件
|
@@ -72,20 +52,13 @@ module CarrierWave
|
|
72
52
|
# returns:
|
73
53
|
# file data
|
74
54
|
def get(path)
|
75
|
-
path
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
"Date" => date,
|
83
|
-
"Authorization" => sign("GET", bucket_path, "", "" ,date)
|
84
|
-
}
|
85
|
-
|
86
|
-
# path = format_path(path)
|
87
|
-
# url = path_to_url(path)
|
88
|
-
RestClient.get(url, headers)
|
55
|
+
path.sub!(/^\//, '')
|
56
|
+
res = oss_upload_client.bucket_get_object(path)
|
57
|
+
if res.success?
|
58
|
+
return res.parsed_response
|
59
|
+
else
|
60
|
+
raise "Get content faild"
|
61
|
+
end
|
89
62
|
end
|
90
63
|
|
91
64
|
# 删除 Remote 的文件
|
@@ -96,57 +69,53 @@ module CarrierWave
|
|
96
69
|
# returns:
|
97
70
|
# 图片的下载地址
|
98
71
|
def delete(path)
|
99
|
-
path
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
"Date" => date,
|
107
|
-
"Authorization" => sign("DELETE", bucket_path, "", "" ,date)
|
108
|
-
}
|
109
|
-
|
110
|
-
RestClient.delete(url, headers)
|
111
|
-
return path_to_url(path, :get => true)
|
72
|
+
path.sub!(/^\//, '')
|
73
|
+
res = oss_upload_client.bucket_delete_object(path)
|
74
|
+
if res.success?
|
75
|
+
return path_to_url(path)
|
76
|
+
else
|
77
|
+
raise "Delete failed"
|
78
|
+
end
|
112
79
|
end
|
113
80
|
|
114
|
-
|
115
|
-
#
|
116
|
-
def
|
117
|
-
|
81
|
+
##
|
82
|
+
# 根据配置返回完整的上传文件的访问地址
|
83
|
+
def path_to_url(path)
|
84
|
+
[@aliyun_host, path].join("/")
|
118
85
|
end
|
119
86
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
path
|
87
|
+
# 私有空间访问地址,会带上实时算出的 token 信息
|
88
|
+
# 有效期 3600s
|
89
|
+
def private_get_url(path)
|
90
|
+
path.sub!(/^\//, '')
|
91
|
+
oss_client.bucket_get_object_share_link(path, 3600)
|
125
92
|
end
|
126
93
|
|
127
|
-
|
128
|
-
|
94
|
+
private
|
95
|
+
def oss_client
|
96
|
+
return @oss_client if defined?(@oss_client)
|
97
|
+
opts = {
|
98
|
+
host: "oss-#{@aliyun_area}.aliyuncs.com",
|
99
|
+
bucket: @aliyun_bucket
|
100
|
+
}
|
101
|
+
@oss_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
129
102
|
end
|
130
103
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
if
|
135
|
-
"
|
104
|
+
def oss_upload_client
|
105
|
+
return @oss_upload_client if defined?(@oss_upload_client)
|
106
|
+
|
107
|
+
if @uploader.aliyun_internal
|
108
|
+
host = "oss-#{@aliyun_area}-internal.aliyuncs.com"
|
136
109
|
else
|
137
|
-
"
|
110
|
+
host = "oss-#{@aliyun_area}.aliyuncs.com"
|
138
111
|
end
|
139
|
-
end
|
140
112
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
h = OpenSSL::HMAC.digest(digest, @aliyun_access_key, string_to_sign)
|
148
|
-
h = Base64.encode64(h)
|
149
|
-
"OSS #{@aliyun_access_id}:#{h}"
|
113
|
+
opts = {
|
114
|
+
host: host,
|
115
|
+
bucket: @aliyun_bucket
|
116
|
+
}
|
117
|
+
|
118
|
+
@oss_upload_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
150
119
|
end
|
151
120
|
end
|
152
121
|
|
@@ -196,7 +165,11 @@ module CarrierWave
|
|
196
165
|
end
|
197
166
|
|
198
167
|
def url
|
199
|
-
|
168
|
+
if @uploader.aliyun_private_read
|
169
|
+
oss_connection.private_get_url(@path)
|
170
|
+
else
|
171
|
+
oss_connection.path_to_url(@path)
|
172
|
+
end
|
200
173
|
end
|
201
174
|
|
202
175
|
def content_type
|
@@ -214,7 +187,7 @@ module CarrierWave
|
|
214
187
|
private
|
215
188
|
|
216
189
|
def headers
|
217
|
-
@headers ||= {
|
190
|
+
@headers ||= {}
|
218
191
|
end
|
219
192
|
|
220
193
|
def connection
|
@@ -222,18 +195,9 @@ module CarrierWave
|
|
222
195
|
end
|
223
196
|
|
224
197
|
def oss_connection
|
225
|
-
return @oss_connection if @oss_connection
|
226
|
-
|
227
|
-
|
228
|
-
:aliyun_access_id => @uploader.aliyun_access_id,
|
229
|
-
:aliyun_access_key => @uploader.aliyun_access_key,
|
230
|
-
:aliyun_area => @uploader.aliyun_area,
|
231
|
-
:aliyun_bucket => @uploader.aliyun_bucket,
|
232
|
-
:aliyun_internal => @uploader.aliyun_internal,
|
233
|
-
:aliyun_host => @uploader.aliyun_host,
|
234
|
-
:aliyun_upload_host => @uploader.aliyun_upload_host
|
235
|
-
}
|
236
|
-
@oss_connection ||= CarrierWave::Storage::Aliyun::Connection.new(config)
|
198
|
+
return @oss_connection if defined? @oss_connection
|
199
|
+
|
200
|
+
@oss_connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
237
201
|
end
|
238
202
|
|
239
203
|
end
|
data/spec/aliyun_spec.rb
CHANGED
@@ -12,40 +12,59 @@ describe "Aliyun" do
|
|
12
12
|
:aliyun_internal => true,
|
13
13
|
:aliyun_host => "http://bison-dev.cn-hangzhou.oss.aliyun-inc.com"
|
14
14
|
}
|
15
|
-
|
15
|
+
|
16
|
+
@uploader = CarrierWave::Uploader::Base.new
|
17
|
+
@connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should put" do
|
19
21
|
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
20
|
-
Net::HTTP.get_response(URI.parse(url))
|
22
|
+
res = Net::HTTP.get_response(URI.parse(url))
|
23
|
+
expect(res.code).to eq "200"
|
21
24
|
end
|
22
25
|
|
23
26
|
it "should put with / prefix" do
|
24
27
|
url = @connection.put("/a/a.jpg",load_file("foo.jpg"))
|
25
|
-
Net::HTTP.get_response(URI.parse(url))
|
28
|
+
res = Net::HTTP.get_response(URI.parse(url))
|
29
|
+
expect(res.code).to eq "200"
|
26
30
|
end
|
27
|
-
|
31
|
+
|
28
32
|
it "should delete" do
|
29
33
|
url = @connection.delete("/a/a.jpg")
|
30
|
-
Net::HTTP.get_response(URI.parse(url))
|
34
|
+
res = Net::HTTP.get_response(URI.parse(url))
|
35
|
+
expect(res.code).to eq "404"
|
31
36
|
end
|
32
|
-
|
37
|
+
|
33
38
|
it "should support custom domain" do
|
34
|
-
@
|
35
|
-
@connection = CarrierWave::Storage::Aliyun::Connection.new(@
|
39
|
+
@uploader.aliyun_host = "https://foo.bar.com"
|
40
|
+
@connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
36
41
|
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
37
|
-
url.
|
38
|
-
@
|
39
|
-
@connection = CarrierWave::Storage::Aliyun::Connection.new(@
|
42
|
+
expect(url).to eq "https://foo.bar.com/a/a.jpg"
|
43
|
+
@uploader.aliyun_host = "http://foo.bar.com"
|
44
|
+
@connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
40
45
|
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
41
|
-
url.
|
46
|
+
expect(url).to eq "http://foo.bar.com/a/a.jpg"
|
42
47
|
end
|
43
|
-
|
48
|
+
|
49
|
+
describe 'private read bucket' do
|
50
|
+
before do
|
51
|
+
@uploader.aliyun_private_read = true
|
52
|
+
@connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should get url include token' do
|
56
|
+
url = @connection.private_get_url('bar/foo.jpg')
|
57
|
+
# http://oss-cn-beijing.aliyuncs.com.//carrierwave-aliyun-test.oss-cn-beijing.aliyuncs.com/bar/foo.jpg?OSSAccessKeyId=1OpWEtPTjIDv5u8q&Expires=1455172009&Signature=4ibgQpfHOjVpqxG6162S8Ar3c6c=
|
58
|
+
expect(url).to include(*%w(Signature Expires OSSAccessKeyId))
|
59
|
+
expect(url).to include "http://#{@uploader.aliyun_bucket}.oss-cn-beijing.aliyuncs.com/bar/foo.jpg"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
44
63
|
describe 'File' do
|
45
64
|
it 'should have respond_to identifier' do
|
46
|
-
f = CarrierWave::Storage::Aliyun::File.new(
|
47
|
-
f.
|
48
|
-
f.
|
65
|
+
f = CarrierWave::Storage::Aliyun::File.new(@uploader, '', '')
|
66
|
+
expect(f).to respond_to(:identifier)
|
67
|
+
expect(f).to respond_to(:filename)
|
49
68
|
end
|
50
69
|
end
|
51
70
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rspec'
|
3
|
-
require 'rspec/autorun'
|
4
3
|
require 'rails'
|
5
4
|
require 'active_record'
|
6
5
|
require "carrierwave"
|
@@ -25,20 +24,20 @@ ActiveRecord::Migration.verbose = false
|
|
25
24
|
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
26
25
|
|
27
26
|
# 测试的时候需要修改这个地方
|
28
|
-
ALIYUN_ACCESS_ID = "
|
29
|
-
ALIYUN_ACCESS_KEY = '
|
30
|
-
ALIYUN_BUCKET = "
|
27
|
+
ALIYUN_ACCESS_ID = "1OpWEtPTjIDv5u8q"
|
28
|
+
ALIYUN_ACCESS_KEY = 'cz12XgPfEVy8Xe9F9UJJHmVdHBJ9bi'
|
29
|
+
ALIYUN_BUCKET = "carrierwave-aliyun-test"
|
30
|
+
ALIYUN_AREA = "cn-beijing"
|
31
31
|
|
32
32
|
CarrierWave.configure do |config|
|
33
33
|
config.storage = :aliyun
|
34
34
|
config.aliyun_access_id = ALIYUN_ACCESS_ID
|
35
35
|
config.aliyun_access_key = ALIYUN_ACCESS_KEY
|
36
36
|
config.aliyun_bucket = ALIYUN_BUCKET
|
37
|
-
config.aliyun_area =
|
38
|
-
config.aliyun_internal =
|
39
|
-
config.aliyun_host = "http://bison-dev.cn-hangzhou.oss.aliyun-inc.com"
|
37
|
+
config.aliyun_area = ALIYUN_AREA
|
38
|
+
config.aliyun_internal = false
|
40
39
|
end
|
41
40
|
|
42
41
|
def load_file(fname)
|
43
42
|
File.open([Rails.root,fname].join("/"))
|
44
|
-
end
|
43
|
+
end
|
data/spec/upload_spec.rb
CHANGED
@@ -10,31 +10,31 @@ describe "Upload" do
|
|
10
10
|
create_table :photos do |t|
|
11
11
|
t.column :image, :string
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
create_table :attachments do |t|
|
15
15
|
t.column :file, :string
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def drop_db
|
21
21
|
ActiveRecord::Base.connection.tables.each do |table|
|
22
22
|
ActiveRecord::Base.connection.drop_table(table)
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
class PhotoUploader < CarrierWave::Uploader::Base
|
27
27
|
include CarrierWave::MiniMagick
|
28
28
|
|
29
29
|
version :small do
|
30
30
|
process :resize_to_fill => [120, 120]
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def store_dir
|
34
34
|
"photos"
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
class AttachUploader < CarrierWave::Uploader::Base
|
39
39
|
include CarrierWave::MiniMagick
|
40
40
|
|
@@ -46,20 +46,20 @@ describe "Upload" do
|
|
46
46
|
class Photo < ActiveRecord::Base
|
47
47
|
mount_uploader :image, PhotoUploader
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
class Attachment < ActiveRecord::Base
|
51
51
|
mount_uploader :file, AttachUploader
|
52
52
|
end
|
53
|
-
|
54
|
-
|
53
|
+
|
54
|
+
|
55
55
|
before :all do
|
56
56
|
setup_db
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
after :all do
|
60
60
|
drop_db
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
describe "Upload Image" do
|
64
64
|
context "should upload image" do
|
65
65
|
before(:all) do
|
@@ -68,46 +68,47 @@ describe "Upload" do
|
|
68
68
|
@photo = Photo.new(:image => @file)
|
69
69
|
@photo1 = Photo.new(:image => @file1)
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
it "should upload file" do
|
73
|
-
@photo.save.
|
74
|
-
@photo1.save.
|
73
|
+
expect(@photo.save).to eq true
|
74
|
+
expect(@photo1.save).to eq true
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "should get uploaded file" do
|
78
78
|
img = open(@photo.image.url)
|
79
|
-
img.size.
|
79
|
+
expect(img.size).to eq @file.size
|
80
80
|
img1 = open(@photo1.image.url)
|
81
|
-
img1.size.
|
81
|
+
expect(img1.size).to eq @file1.size
|
82
82
|
end
|
83
|
-
|
84
|
-
it "sholud get small version uploaded file" do
|
85
|
-
open(@photo.image.small.url).
|
86
|
-
open(@photo1.image.small.url).
|
83
|
+
|
84
|
+
it "sholud get small version uploaded file" do
|
85
|
+
expect(open(@photo.image.small.url)).not_to eq nil
|
86
|
+
expect(open(@photo1.image.small.url)).not_to eq nil
|
87
87
|
end
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
context "should update zip" do
|
91
91
|
before(:all) do
|
92
92
|
@file = load_file("foo.zip")
|
93
93
|
@attachment = Attachment.new(:file => @file)
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
it "should upload file" do
|
97
|
-
@attachment.save.
|
97
|
+
expect(@attachment.save).to eq true
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
it "should get uploaded file" do
|
101
101
|
attach = open(@attachment.file.url)
|
102
|
-
attach.size.
|
102
|
+
expect(attach.size).to eq @file.size
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
it "should delete old file when upload a new file again" do
|
106
106
|
old_url = @attachment.file.url
|
107
107
|
@attachment.file = load_file("foo.gif")
|
108
108
|
@attachment.save
|
109
|
-
Net::HTTP.get_response(URI.parse(old_url))
|
109
|
+
res = Net::HTTP.get_response(URI.parse(old_url))
|
110
|
+
expect(res.code).to eq "404"
|
110
111
|
end
|
111
112
|
end
|
112
113
|
end
|
113
|
-
end
|
114
|
+
end
|
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
|
+
version: 0.4.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:
|
11
|
+
date: 2016-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -25,19 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.5.7
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: aliyun-oss-sdk
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.
|
33
|
+
version: 0.1.5
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1.
|
40
|
+
version: 0.1.5
|
41
41
|
description: Aliyun OSS support for Carrierwave
|
42
42
|
email:
|
43
43
|
- huacnlee@gmail.com
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
84
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
85
|
+
rubygems_version: 2.5.1
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: Aliyun OSS support for Carrierwave
|