carrierwave-aliyun 0.5.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +53 -0
- data/.travis.yml +0 -1
- data/CHANGELOG.md +10 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +50 -47
- data/README.md +20 -1
- data/carrierwave-aliyun.gemspec +11 -11
- data/lib/carrierwave-aliyun.rb +3 -3
- data/lib/carrierwave/aliyun/bucket.rb +147 -0
- data/lib/carrierwave/aliyun/configuration.rb +3 -22
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +8 -222
- data/lib/carrierwave/storage/aliyun_file.rb +65 -0
- data/spec/aliyun_spec.rb +32 -34
- data/spec/spec_helper.rb +8 -6
- data/spec/upload_spec.rb +33 -29
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a099dd1e65c2265f3c61a400c508240e49d62116
|
4
|
+
data.tar.gz: b62b42570960b74d220ca9052f190d3469339eae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e04cb4af3cad79a767a0972dc5f2fb918b5934d7d8e51c4599cddfe788df24bf28919b71ccde25c681a0a4ee59bedce40bb7801d1f0b5d9fdd8f7a36b3e22292
|
7
|
+
data.tar.gz: 9a5d2c87ea663865a2b0d4a246f1398ce0e045bd1cc8c6599f3e6c1d38e978c9640aa8e725fba10a9055ea2e7367cb4b98372b48044b0124fcb4764d09a581e3
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Style/AsciiComments:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/IfUnlessModifier:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Style/RedundantSelf:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/RaiseArgs:
|
14
|
+
EnforcedStyle: compact
|
15
|
+
|
16
|
+
Style/MutableConstant:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/LineLength:
|
20
|
+
Max: 1200
|
21
|
+
|
22
|
+
Metrics/ClassLength:
|
23
|
+
Max: 1200
|
24
|
+
|
25
|
+
Metrics/MethodLength:
|
26
|
+
Max: 1200
|
27
|
+
|
28
|
+
Metrics/ModuleLength:
|
29
|
+
Max: 1200
|
30
|
+
|
31
|
+
Metrics/CyclomaticComplexity:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Metrics/AbcSize:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Metrics/PerceivedComplexity:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Rails/TimeZone:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/GlobalVars:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/GuardClause:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Rails/FindBy:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Rails/HasAndBelongsToMany:
|
53
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
## 0.7.0
|
2
|
+
|
3
|
+
- 支持设置 Content-Disposition;
|
4
|
+
|
5
|
+
## 0.6.0
|
6
|
+
|
7
|
+
- 调整,优化类结构:
|
8
|
+
- `CarrierWave::Storage::Aliyun::Connection` -> `CarrierWave::Aliyun::Bucket`
|
9
|
+
- `CarrierWave::Storage::Aliyun::File` -> `CarrierWave::Storage::AliyunFile`
|
10
|
+
|
1
11
|
## 0.5.1
|
2
12
|
|
3
13
|
- 修正 Aliyun 内部网络上传的支持;(#36)
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
carrierwave-aliyun (0.
|
4
|
+
carrierwave-aliyun (0.7.0)
|
5
5
|
aliyun-oss-sdk (>= 0.1.6)
|
6
6
|
carrierwave (>= 0.5.7)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionmailer (4.2.
|
12
|
-
actionpack (= 4.2.
|
13
|
-
actionview (= 4.2.
|
14
|
-
activejob (= 4.2.
|
11
|
+
actionmailer (4.2.6)
|
12
|
+
actionpack (= 4.2.6)
|
13
|
+
actionview (= 4.2.6)
|
14
|
+
activejob (= 4.2.6)
|
15
15
|
mail (~> 2.5, >= 2.5.4)
|
16
16
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
17
|
-
actionpack (4.2.
|
18
|
-
actionview (= 4.2.
|
19
|
-
activesupport (= 4.2.
|
20
|
-
rack (~> 1.6
|
17
|
+
actionpack (4.2.6)
|
18
|
+
actionview (= 4.2.6)
|
19
|
+
activesupport (= 4.2.6)
|
20
|
+
rack (~> 1.6)
|
21
21
|
rack-test (~> 0.6.2)
|
22
22
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
23
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.
|
24
|
-
actionview (4.2.
|
25
|
-
activesupport (= 4.2.
|
23
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
24
|
+
actionview (4.2.6)
|
25
|
+
activesupport (= 4.2.6)
|
26
26
|
builder (~> 3.1)
|
27
27
|
erubis (~> 2.7.0)
|
28
28
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
29
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.
|
30
|
-
activejob (4.2.
|
31
|
-
activesupport (= 4.2.
|
29
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
30
|
+
activejob (4.2.6)
|
31
|
+
activesupport (= 4.2.6)
|
32
32
|
globalid (>= 0.3.0)
|
33
|
-
activemodel (4.2.
|
34
|
-
activesupport (= 4.2.
|
33
|
+
activemodel (4.2.6)
|
34
|
+
activesupport (= 4.2.6)
|
35
35
|
builder (~> 3.1)
|
36
|
-
activerecord (4.2.
|
37
|
-
activemodel (= 4.2.
|
38
|
-
activesupport (= 4.2.
|
36
|
+
activerecord (4.2.6)
|
37
|
+
activemodel (= 4.2.6)
|
38
|
+
activesupport (= 4.2.6)
|
39
39
|
arel (~> 6.0)
|
40
|
-
activesupport (4.2.
|
40
|
+
activesupport (4.2.6)
|
41
41
|
i18n (~> 0.7)
|
42
42
|
json (~> 1.7, >= 1.7.7)
|
43
43
|
minitest (~> 5.1)
|
@@ -56,43 +56,46 @@ GEM
|
|
56
56
|
json (>= 1.7)
|
57
57
|
mime-types (>= 1.16)
|
58
58
|
mimemagic (>= 0.3.0)
|
59
|
-
concurrent-ruby (1.0.
|
59
|
+
concurrent-ruby (1.0.2)
|
60
60
|
diff-lcs (1.2.5)
|
61
61
|
erubis (2.7.0)
|
62
62
|
globalid (0.3.6)
|
63
63
|
activesupport (>= 4.1.0)
|
64
64
|
gyoku (1.3.1)
|
65
65
|
builder (>= 2.1.2)
|
66
|
-
httparty (0.
|
67
|
-
json (~> 1.8)
|
66
|
+
httparty (0.14.0)
|
68
67
|
multi_xml (>= 0.5.2)
|
69
68
|
i18n (0.7.0)
|
70
69
|
json (1.8.3)
|
71
70
|
loofah (2.0.3)
|
72
71
|
nokogiri (>= 1.5.9)
|
73
|
-
mail (2.6.
|
74
|
-
mime-types (>= 1.16, <
|
75
|
-
mime-types (
|
76
|
-
|
72
|
+
mail (2.6.4)
|
73
|
+
mime-types (>= 1.16, < 4)
|
74
|
+
mime-types (3.1)
|
75
|
+
mime-types-data (~> 3.2015)
|
76
|
+
mime-types-data (3.2016.0521)
|
77
|
+
mimemagic (0.3.2)
|
77
78
|
mini_magick (4.4.0)
|
78
|
-
mini_portile2 (2.
|
79
|
-
minitest (5.
|
79
|
+
mini_portile2 (2.1.0)
|
80
|
+
minitest (5.9.0)
|
80
81
|
multi_xml (0.5.5)
|
81
|
-
nokogiri (1.6.
|
82
|
-
mini_portile2 (~> 2.
|
82
|
+
nokogiri (1.6.8)
|
83
|
+
mini_portile2 (~> 2.1.0)
|
84
|
+
pkg-config (~> 1.1.7)
|
85
|
+
pkg-config (1.1.7)
|
83
86
|
rack (1.6.4)
|
84
87
|
rack-test (0.6.3)
|
85
88
|
rack (>= 1.0)
|
86
|
-
rails (4.2.
|
87
|
-
actionmailer (= 4.2.
|
88
|
-
actionpack (= 4.2.
|
89
|
-
actionview (= 4.2.
|
90
|
-
activejob (= 4.2.
|
91
|
-
activemodel (= 4.2.
|
92
|
-
activerecord (= 4.2.
|
93
|
-
activesupport (= 4.2.
|
89
|
+
rails (4.2.6)
|
90
|
+
actionmailer (= 4.2.6)
|
91
|
+
actionpack (= 4.2.6)
|
92
|
+
actionview (= 4.2.6)
|
93
|
+
activejob (= 4.2.6)
|
94
|
+
activemodel (= 4.2.6)
|
95
|
+
activerecord (= 4.2.6)
|
96
|
+
activesupport (= 4.2.6)
|
94
97
|
bundler (>= 1.3.0, < 2.0)
|
95
|
-
railties (= 4.2.
|
98
|
+
railties (= 4.2.6)
|
96
99
|
sprockets-rails
|
97
100
|
rails-deprecated_sanitizer (1.0.3)
|
98
101
|
activesupport (>= 4.2.0.alpha)
|
@@ -102,9 +105,9 @@ GEM
|
|
102
105
|
rails-deprecated_sanitizer (>= 1.0.1)
|
103
106
|
rails-html-sanitizer (1.0.3)
|
104
107
|
loofah (~> 2.0)
|
105
|
-
railties (4.2.
|
106
|
-
actionpack (= 4.2.
|
107
|
-
activesupport (= 4.2.
|
108
|
+
railties (4.2.6)
|
109
|
+
actionpack (= 4.2.6)
|
110
|
+
activesupport (= 4.2.6)
|
108
111
|
rake (>= 0.8.7)
|
109
112
|
thor (>= 0.18.1, < 2.0)
|
110
113
|
rake (11.2.2)
|
@@ -121,10 +124,10 @@ GEM
|
|
121
124
|
diff-lcs (>= 1.2.0, < 2.0)
|
122
125
|
rspec-support (~> 3.4.0)
|
123
126
|
rspec-support (3.4.1)
|
124
|
-
sprockets (3.
|
127
|
+
sprockets (3.6.2)
|
125
128
|
concurrent-ruby (~> 1.0)
|
126
129
|
rack (> 1, < 3)
|
127
|
-
sprockets-rails (3.
|
130
|
+
sprockets-rails (3.1.1)
|
128
131
|
actionpack (>= 4.0)
|
129
132
|
activesupport (>= 4.0)
|
130
133
|
sprockets (>= 3.0.0)
|
@@ -142,7 +145,7 @@ PLATFORMS
|
|
142
145
|
DEPENDENCIES
|
143
146
|
carrierwave-aliyun!
|
144
147
|
mini_magick
|
145
|
-
rails (
|
148
|
+
rails (~> 4.2.0)
|
146
149
|
rake
|
147
150
|
rspec
|
148
151
|
sqlite3-ruby
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
This gem adds support for [Aliyun OSS](http://oss.aliyun.com) to [CarrierWave](https://github.com/jnicklas/carrierwave/)
|
4
4
|
|
5
|
-
[![Gem Version](https://badge.fury.io/rb/carrierwave-aliyun.svg)](https://rubygems.org/gems/carrierwave-aliyun) [![Build Status](https://travis-ci.org/huacnlee/carrierwave-aliyun.svg?branch=master)](https://travis-ci.org/huacnlee/carrierwave-aliyun)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/carrierwave-aliyun.svg)](https://rubygems.org/gems/carrierwave-aliyun) [![Build Status](https://travis-ci.org/huacnlee/carrierwave-aliyun.svg?branch=master)](https://travis-ci.org/huacnlee/carrierwave-aliyun) [![Code Climate](https://codeclimate.com/github/huacnlee/carrierwave-aliyun/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/carrierwave-aliyun)
|
6
6
|
|
7
7
|
> NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用,如果你需要直接用 Aliyun OSS,可以尝试用 [aliyun-oss-ruby-sdk](https://github.com/aliyun-beta/aliyun-oss-ruby-sdk) 这个 Gem。
|
8
8
|
|
@@ -55,3 +55,22 @@ https://simple.img-cn-hangzhou.aliyuncs.com/users/avatar/12.png@100w_200h_1c_95q
|
|
55
55
|
# 你也可以用自定义的缩略图格式
|
56
56
|
irb> User.last.avatar.url(thumb: '@!large')
|
57
57
|
```
|
58
|
+
|
59
|
+
## 增对文件设置 Content-Disposition
|
60
|
+
|
61
|
+
在文件上传的场景(非图片),你可能需要给上传的文件设置 Content-Disposition 以便于用户直接访问 URL 的时候能够用你期望的文件名或原文件名来下载并保存。
|
62
|
+
|
63
|
+
这个时候你需要给 Uploader 实现 `content_disposition` 函数,例如:
|
64
|
+
|
65
|
+
```rb
|
66
|
+
# app/uploaders/attachment_uploader.rb
|
67
|
+
class AttachmentUploader < CarrierWave::Uploader::Base
|
68
|
+
def content_disposition
|
69
|
+
# 非图片文件,给 content_disposition
|
70
|
+
unless file.extension.downcase.in?(%w(jpg jpeg gif png svg))
|
71
|
+
"attachment;filename=#{file.original_filename}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
```
|
data/carrierwave-aliyun.gemspec
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
3
3
|
require File.expand_path('lib/carrierwave/aliyun/version')
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'carrierwave-aliyun'
|
7
7
|
s.version = CarrierWave::Aliyun::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
12
|
-
s.summary =
|
13
|
-
s.description =
|
9
|
+
s.authors = ['Jason Lee']
|
10
|
+
s.email = ['huacnlee@gmail.com']
|
11
|
+
s.homepage = 'https://github.com/huacnlee/carrierwave-aliyun'
|
12
|
+
s.summary = 'Aliyun OSS support for Carrierwave'
|
13
|
+
s.description = 'Aliyun OSS support for Carrierwave'
|
14
14
|
s.files = `git ls-files`.split("\n")
|
15
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
-
s.require_paths = [
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
17
|
+
s.require_paths = ['lib']
|
18
18
|
s.license = 'MIT'
|
19
19
|
|
20
|
-
s.add_dependency
|
21
|
-
s.add_dependency
|
20
|
+
s.add_dependency 'carrierwave', ['>= 0.5.7']
|
21
|
+
s.add_dependency 'aliyun-oss-sdk', ['>= 0.1.6']
|
22
22
|
end
|
data/lib/carrierwave-aliyun.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'carrierwave/storage/aliyun'
|
2
|
+
require 'carrierwave/storage/aliyun_file'
|
3
|
+
require 'carrierwave/aliyun/bucket'
|
4
|
+
require 'carrierwave/aliyun/version'
|
2
5
|
require 'carrierwave/aliyun/configuration'
|
3
6
|
|
4
|
-
CarrierWave.configure do |config|
|
5
|
-
config.storage_engines.merge!({ aliyun: 'CarrierWave::Storage::Aliyun' })
|
6
|
-
end
|
7
7
|
CarrierWave::Uploader::Base.send(:include, CarrierWave::Aliyun::Configuration)
|
8
8
|
|
9
9
|
if CarrierWave::VERSION <= '0.11.0'
|
@@ -0,0 +1,147 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Aliyun
|
3
|
+
class Bucket
|
4
|
+
PATH_PREFIX = %r{^/}
|
5
|
+
|
6
|
+
def initialize(uploader)
|
7
|
+
@aliyun_access_id = uploader.aliyun_access_id
|
8
|
+
@aliyun_access_key = uploader.aliyun_access_key
|
9
|
+
@aliyun_bucket = uploader.aliyun_bucket
|
10
|
+
@aliyun_area = uploader.aliyun_area || 'cn-hangzhou'
|
11
|
+
@aliyun_private_read = uploader.aliyun_private_read
|
12
|
+
@aliyun_internal = uploader.aliyun_internal
|
13
|
+
|
14
|
+
# Host for get request
|
15
|
+
@aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
16
|
+
@aliyun_img_host = uploader.aliyun_img_host || "http://#{@aliyun_bucket}.img-#{@aliyun_area}.aliyuncs.com"
|
17
|
+
|
18
|
+
unless @aliyun_host.include?('//')
|
19
|
+
raise "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# 上传文件
|
24
|
+
# params:
|
25
|
+
# - path - remote 存储路径
|
26
|
+
# - file - 需要上传文件的 File 对象
|
27
|
+
# - opts:
|
28
|
+
# - content_type - 上传文件的 MimeType,默认 `image/jpg`
|
29
|
+
# - content_disposition - Content-Disposition
|
30
|
+
# returns:
|
31
|
+
# 图片的下载地址
|
32
|
+
def put(path, file, opts = {})
|
33
|
+
path.sub!(PATH_PREFIX, '')
|
34
|
+
|
35
|
+
headers = {}
|
36
|
+
headers['Content-Type'] = opts[:content_type] || 'image/jpg'
|
37
|
+
content_disposition = opts[:content_disposition]
|
38
|
+
if content_disposition
|
39
|
+
headers['Content-Disposition'] = content_disposition
|
40
|
+
end
|
41
|
+
|
42
|
+
res = oss_upload_client.bucket_create_object(path, file, headers)
|
43
|
+
if res.success?
|
44
|
+
path_to_url(path)
|
45
|
+
else
|
46
|
+
raise 'Put file failed'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# 读取文件
|
51
|
+
# params:
|
52
|
+
# - path - remote 存储路径
|
53
|
+
# returns:
|
54
|
+
# file data
|
55
|
+
def get(path)
|
56
|
+
path.sub!(PATH_PREFIX, '')
|
57
|
+
res = oss_upload_client.bucket_get_object(path)
|
58
|
+
if res.success?
|
59
|
+
return res.parsed_response
|
60
|
+
else
|
61
|
+
raise 'Get content faild'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# 删除 Remote 的文件
|
66
|
+
#
|
67
|
+
# params:
|
68
|
+
# - path - remote 存储路径
|
69
|
+
#
|
70
|
+
# returns:
|
71
|
+
# 图片的下载地址
|
72
|
+
def delete(path)
|
73
|
+
path.sub!(PATH_PREFIX, '')
|
74
|
+
res = oss_upload_client.bucket_delete_object(path)
|
75
|
+
if res.success?
|
76
|
+
return path_to_url(path)
|
77
|
+
else
|
78
|
+
raise 'Delete failed'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# 根据配置返回完整的上传文件的访问地址
|
84
|
+
def path_to_url(path, opts = {})
|
85
|
+
if opts[:thumb]
|
86
|
+
thumb_path = [path, opts[:thumb]].join('')
|
87
|
+
[@aliyun_img_host, thumb_path].join('/')
|
88
|
+
else
|
89
|
+
[@aliyun_host, path].join('/')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# 私有空间访问地址,会带上实时算出的 token 信息
|
94
|
+
# 有效期 3600s
|
95
|
+
def private_get_url(path, opts = {})
|
96
|
+
path.sub!(PATH_PREFIX, '')
|
97
|
+
if opts[:thumb]
|
98
|
+
thumb_path = [path, opts[:thumb]].join('')
|
99
|
+
img_client.bucket_get_object_share_link(thumb_path, 3600)
|
100
|
+
else
|
101
|
+
oss_client.bucket_get_object_share_link(path, 3600)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def head(path)
|
106
|
+
oss_client.bucket_get_meta_object(path)
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def oss_client
|
112
|
+
return @oss_client if defined?(@oss_client)
|
113
|
+
opts = {
|
114
|
+
host: "oss-#{@aliyun_area}.aliyuncs.com",
|
115
|
+
bucket: @aliyun_bucket
|
116
|
+
}
|
117
|
+
@oss_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
118
|
+
end
|
119
|
+
|
120
|
+
def img_client
|
121
|
+
return @img_client if defined?(@img_client)
|
122
|
+
opts = {
|
123
|
+
host: "img-#{@aliyun_area}.aliyuncs.com",
|
124
|
+
bucket: @aliyun_bucket
|
125
|
+
}
|
126
|
+
@img_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
127
|
+
end
|
128
|
+
|
129
|
+
def oss_upload_client
|
130
|
+
return @oss_upload_client if defined?(@oss_upload_client)
|
131
|
+
|
132
|
+
host = if @aliyun_internal
|
133
|
+
"oss-#{@aliyun_area}-internal.aliyuncs.com"
|
134
|
+
else
|
135
|
+
"oss-#{@aliyun_area}.aliyuncs.com"
|
136
|
+
end
|
137
|
+
|
138
|
+
opts = {
|
139
|
+
host: host,
|
140
|
+
bucket: @aliyun_bucket
|
141
|
+
}
|
142
|
+
|
143
|
+
@oss_upload_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -12,29 +12,10 @@ module CarrierWave
|
|
12
12
|
add_config :aliyun_host
|
13
13
|
add_config :aliyun_img_host
|
14
14
|
add_config :aliyun_private_read
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
module ClassMethods
|
19
|
-
def add_config(name)
|
20
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
21
|
-
def self.#{name}(value=nil)
|
22
|
-
@#{name} = value if value
|
23
|
-
return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
|
24
|
-
name = superclass.#{name}
|
25
|
-
return nil if name.nil? && !instance_variable_defined?("@#{name}")
|
26
|
-
@#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.#{name}=(value)
|
30
|
-
@#{name} = value
|
31
|
-
end
|
32
15
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
RUBY
|
16
|
+
configure do |config|
|
17
|
+
config.storage_engines[:aliyun] = 'CarrierWave::Storage::Aliyun'
|
18
|
+
end
|
38
19
|
end
|
39
20
|
end
|
40
21
|
end
|
@@ -5,233 +5,19 @@ require 'uri'
|
|
5
5
|
module CarrierWave
|
6
6
|
module Storage
|
7
7
|
class Aliyun < Abstract
|
8
|
-
class Connection
|
9
|
-
PATH_PREFIX = %r{^/}
|
10
|
-
|
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
|
18
|
-
@aliyun_internal = uploader.aliyun_internal
|
19
|
-
|
20
|
-
# Host for get request
|
21
|
-
@aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
22
|
-
@aliyun_img_host = uploader.aliyun_img_host || "http://#{@aliyun_bucket}.img-#{@aliyun_area}.aliyuncs.com"
|
23
|
-
|
24
|
-
unless @aliyun_host.include?('//')
|
25
|
-
fail "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# 上传文件
|
30
|
-
# params:
|
31
|
-
# - path - remote 存储路径
|
32
|
-
# - file - 需要上传文件的 File 对象
|
33
|
-
# - options:
|
34
|
-
# - content_type - 上传文件的 MimeType,默认 `image/jpg`
|
35
|
-
# returns:
|
36
|
-
# 图片的下载地址
|
37
|
-
def put(path, file, options = {})
|
38
|
-
path.sub!(PATH_PREFIX, '')
|
39
|
-
opts = {
|
40
|
-
'Content-Type' => options[:content_type] || 'image/jpg'
|
41
|
-
}
|
42
|
-
|
43
|
-
res = oss_upload_client.bucket_create_object(path, file, opts)
|
44
|
-
if res.success?
|
45
|
-
path_to_url(path)
|
46
|
-
else
|
47
|
-
fail 'Put file failed'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# 读取文件
|
52
|
-
# params:
|
53
|
-
# - path - remote 存储路径
|
54
|
-
# returns:
|
55
|
-
# file data
|
56
|
-
def get(path)
|
57
|
-
path.sub!(PATH_PREFIX, '')
|
58
|
-
res = oss_upload_client.bucket_get_object(path)
|
59
|
-
if res.success?
|
60
|
-
return res.parsed_response
|
61
|
-
else
|
62
|
-
fail 'Get content faild'
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
# 删除 Remote 的文件
|
67
|
-
#
|
68
|
-
# params:
|
69
|
-
# - path - remote 存储路径
|
70
|
-
#
|
71
|
-
# returns:
|
72
|
-
# 图片的下载地址
|
73
|
-
def delete(path)
|
74
|
-
path.sub!(PATH_PREFIX, '')
|
75
|
-
res = oss_upload_client.bucket_delete_object(path)
|
76
|
-
if res.success?
|
77
|
-
return path_to_url(path)
|
78
|
-
else
|
79
|
-
fail 'Delete failed'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
##
|
84
|
-
# 根据配置返回完整的上传文件的访问地址
|
85
|
-
def path_to_url(path, opts = {})
|
86
|
-
if opts[:thumb]
|
87
|
-
thumb_path = [path, opts[:thumb]].join('')
|
88
|
-
[@aliyun_img_host, thumb_path].join('/')
|
89
|
-
else
|
90
|
-
[@aliyun_host, path].join('/')
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# 私有空间访问地址,会带上实时算出的 token 信息
|
95
|
-
# 有效期 3600s
|
96
|
-
def private_get_url(path, opts = {})
|
97
|
-
path.sub!(PATH_PREFIX, '')
|
98
|
-
if opts[:thumb]
|
99
|
-
thumb_path = [path, opts[:thumb]].join('')
|
100
|
-
img_client.bucket_get_object_share_link(thumb_path, 3600)
|
101
|
-
else
|
102
|
-
oss_client.bucket_get_object_share_link(path, 3600)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def oss_client
|
109
|
-
return @oss_client if defined?(@oss_client)
|
110
|
-
opts = {
|
111
|
-
host: "oss-#{@aliyun_area}.aliyuncs.com",
|
112
|
-
bucket: @aliyun_bucket
|
113
|
-
}
|
114
|
-
@oss_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
115
|
-
end
|
116
|
-
|
117
|
-
def img_client
|
118
|
-
return @img_client if defined?(@img_client)
|
119
|
-
opts = {
|
120
|
-
host: "img-#{@aliyun_area}.aliyuncs.com",
|
121
|
-
bucket: @aliyun_bucket
|
122
|
-
}
|
123
|
-
@img_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
124
|
-
end
|
125
|
-
|
126
|
-
def oss_upload_client
|
127
|
-
return @oss_upload_client if defined?(@oss_upload_client)
|
128
|
-
|
129
|
-
if @aliyun_internal
|
130
|
-
host = "oss-#{@aliyun_area}-internal.aliyuncs.com"
|
131
|
-
else
|
132
|
-
host = "oss-#{@aliyun_area}.aliyuncs.com"
|
133
|
-
end
|
134
|
-
|
135
|
-
opts = {
|
136
|
-
host: host,
|
137
|
-
bucket: @aliyun_bucket
|
138
|
-
}
|
139
|
-
|
140
|
-
@oss_upload_client = ::Aliyun::Oss::Client.new(@aliyun_access_id, @aliyun_access_key, opts)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
class File < CarrierWave::SanitizedFile
|
145
|
-
##
|
146
|
-
# Returns the current path/filename of the file on Cloud Files.
|
147
|
-
#
|
148
|
-
# === Returns
|
149
|
-
#
|
150
|
-
# [String] A path
|
151
|
-
#
|
152
|
-
attr_reader :path
|
153
|
-
|
154
|
-
def initialize(uploader, base, path)
|
155
|
-
@uploader = uploader
|
156
|
-
@path = URI.encode(path)
|
157
|
-
@base = base
|
158
|
-
end
|
159
|
-
|
160
|
-
##
|
161
|
-
# Reads the contents of the file from Cloud Files
|
162
|
-
#
|
163
|
-
# === Returns
|
164
|
-
#
|
165
|
-
# [String] contents of the file
|
166
|
-
#
|
167
|
-
def read
|
168
|
-
object = oss_connection.get(@path)
|
169
|
-
@headers = object.headers
|
170
|
-
object.body
|
171
|
-
end
|
172
|
-
|
173
|
-
##
|
174
|
-
# Remove the file from Cloud Files
|
175
|
-
#
|
176
|
-
def delete
|
177
|
-
oss_connection.delete(@path)
|
178
|
-
true
|
179
|
-
rescue => e
|
180
|
-
# If the file's not there, don't panic
|
181
|
-
puts "carrierwave-aliyun delete file failed: #{e}"
|
182
|
-
nil
|
183
|
-
end
|
184
|
-
|
185
|
-
##
|
186
|
-
# Generate file url
|
187
|
-
# params
|
188
|
-
# :thumb - Aliyun OSS Image Processor option, etc: @100w_200h_95q
|
189
|
-
#
|
190
|
-
def url(opts = {})
|
191
|
-
if @uploader.aliyun_private_read
|
192
|
-
oss_connection.private_get_url(@path, opts)
|
193
|
-
else
|
194
|
-
oss_connection.path_to_url(@path, opts)
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
def content_type
|
199
|
-
headers[:content_type]
|
200
|
-
end
|
201
|
-
|
202
|
-
def content_type=(new_content_type)
|
203
|
-
headers[:content_type] = new_content_type
|
204
|
-
end
|
205
|
-
|
206
|
-
def store(file, opts = {})
|
207
|
-
oss_connection.put(@path, file, opts)
|
208
|
-
end
|
209
|
-
|
210
|
-
private
|
211
|
-
|
212
|
-
def headers
|
213
|
-
@headers ||= {}
|
214
|
-
end
|
215
|
-
|
216
|
-
def connection
|
217
|
-
@base.connection
|
218
|
-
end
|
219
|
-
|
220
|
-
def oss_connection
|
221
|
-
return @oss_connection if defined? @oss_connection
|
222
|
-
|
223
|
-
@oss_connection = CarrierWave::Storage::Aliyun::Connection.new(@uploader)
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
8
|
def store!(file)
|
228
|
-
f =
|
229
|
-
|
9
|
+
f = AliyunFile.new(uploader, self, uploader.store_path)
|
10
|
+
headers = {
|
11
|
+
content_type: file.content_type,
|
12
|
+
content_disposition: uploader.try(:content_disposition)
|
13
|
+
}
|
14
|
+
|
15
|
+
f.store(::File.open(file.file), headers)
|
230
16
|
f
|
231
17
|
end
|
232
18
|
|
233
19
|
def retrieve!(identifier)
|
234
|
-
|
20
|
+
AliyunFile.new(uploader, self, uploader.store_path(identifier))
|
235
21
|
end
|
236
22
|
end
|
237
23
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Storage
|
3
|
+
class AliyunFile < CarrierWave::SanitizedFile
|
4
|
+
attr_reader :path
|
5
|
+
|
6
|
+
def initialize(uploader, base, path)
|
7
|
+
@uploader = uploader
|
8
|
+
@path = URI.encode(path)
|
9
|
+
@base = base
|
10
|
+
end
|
11
|
+
|
12
|
+
def read
|
13
|
+
object = bucket.get(@path)
|
14
|
+
@headers = object.headers
|
15
|
+
object.body
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete
|
19
|
+
bucket.delete(@path)
|
20
|
+
true
|
21
|
+
rescue => e
|
22
|
+
# If the file's not there, don't panic
|
23
|
+
puts "carrierwave-aliyun delete file failed: #{e}"
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Generate file url
|
29
|
+
# params
|
30
|
+
# :thumb - Aliyun OSS Image Processor option, etc: @100w_200h_95q
|
31
|
+
#
|
32
|
+
def url(opts = {})
|
33
|
+
if @uploader.aliyun_private_read
|
34
|
+
bucket.private_get_url(@path, opts)
|
35
|
+
else
|
36
|
+
bucket.path_to_url(@path, opts)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def content_type
|
41
|
+
headers[:content_type]
|
42
|
+
end
|
43
|
+
|
44
|
+
def content_type=(new_content_type)
|
45
|
+
headers[:content_type] = new_content_type
|
46
|
+
end
|
47
|
+
|
48
|
+
def store(file, headers = {})
|
49
|
+
bucket.put(@path, file, headers)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def headers
|
55
|
+
@headers ||= {}
|
56
|
+
end
|
57
|
+
|
58
|
+
def bucket
|
59
|
+
return @bucket if defined? @bucket
|
60
|
+
|
61
|
+
@bucket = CarrierWave::Aliyun::Bucket.new(@uploader)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/aliyun_spec.rb
CHANGED
@@ -1,25 +1,23 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require "open-uri"
|
3
|
-
require "net/http"
|
4
2
|
|
5
|
-
describe
|
3
|
+
describe 'Aliyun' do
|
6
4
|
before(:all) do
|
7
5
|
@opts = {
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
6
|
+
aliyun_access_id: ALIYUN_ACCESS_ID,
|
7
|
+
aliyun_access_key: ALIYUN_ACCESS_KEY,
|
8
|
+
aliyun_bucket: ALIYUN_BUCKET,
|
9
|
+
aliyun_area: ALIYUN_AREA,
|
10
|
+
aliyun_internal: false,
|
11
|
+
aliyun_host: 'http://test.cn-hangzhou.oss.aliyun-inc.com'
|
14
12
|
}
|
15
13
|
|
16
14
|
@uploader = CarrierWave::Uploader::Base.new
|
17
|
-
@
|
15
|
+
@bucket = CarrierWave::Aliyun::Bucket.new(@uploader)
|
18
16
|
end
|
19
17
|
|
20
18
|
# it "should put by internal network" do
|
21
19
|
# @uploader.aliyun_internal = true
|
22
|
-
# @connection = CarrierWave::Storage::Aliyun
|
20
|
+
# @connection = CarrierWave::Storage::Aliyun.new(@uploader)
|
23
21
|
# puts @connection.to_json
|
24
22
|
# url = @connection.put("/a/a.jpg",load_file("foo.jpg"))
|
25
23
|
# res = Net::HTTP.get_response(URI.parse(url))
|
@@ -27,57 +25,57 @@ describe "Aliyun" do
|
|
27
25
|
# expect(res.code).to eq "200"
|
28
26
|
# end
|
29
27
|
|
30
|
-
it
|
31
|
-
url = @
|
28
|
+
it 'should put' do
|
29
|
+
url = @bucket.put('a/a.jpg', load_file('foo.jpg'))
|
32
30
|
res = Net::HTTP.get_response(URI.parse(url))
|
33
|
-
expect(res.code).to eq
|
31
|
+
expect(res.code).to eq '200'
|
34
32
|
end
|
35
33
|
|
36
|
-
it
|
37
|
-
url = @
|
34
|
+
it 'should put with / prefix' do
|
35
|
+
url = @bucket.put('/a/a.jpg', load_file('foo.jpg'))
|
38
36
|
res = Net::HTTP.get_response(URI.parse(url))
|
39
|
-
expect(res.code).to eq
|
37
|
+
expect(res.code).to eq '200'
|
40
38
|
end
|
41
39
|
|
42
|
-
it
|
43
|
-
url = @
|
40
|
+
it 'should delete' do
|
41
|
+
url = @bucket.delete('/a/a.jpg')
|
44
42
|
res = Net::HTTP.get_response(URI.parse(url))
|
45
|
-
expect(res.code).to eq
|
43
|
+
expect(res.code).to eq '404'
|
46
44
|
end
|
47
45
|
|
48
|
-
it
|
49
|
-
@uploader.aliyun_host =
|
50
|
-
@
|
51
|
-
url = @
|
52
|
-
expect(url).to eq
|
53
|
-
@uploader.aliyun_host =
|
54
|
-
@
|
55
|
-
url = @
|
56
|
-
expect(url).to eq
|
46
|
+
it 'should support custom domain' do
|
47
|
+
@uploader.aliyun_host = 'https://foo.bar.com'
|
48
|
+
@bucket = CarrierWave::Aliyun::Bucket.new(@uploader)
|
49
|
+
url = @bucket.put('a/a.jpg', load_file('foo.jpg'))
|
50
|
+
expect(url).to eq 'https://foo.bar.com/a/a.jpg'
|
51
|
+
@uploader.aliyun_host = 'http://foo.bar.com'
|
52
|
+
@bucket = CarrierWave::Aliyun::Bucket.new(@uploader)
|
53
|
+
url = @bucket.put('a/a.jpg', load_file('foo.jpg'))
|
54
|
+
expect(url).to eq 'http://foo.bar.com/a/a.jpg'
|
57
55
|
end
|
58
56
|
|
59
57
|
describe 'private read bucket' do
|
60
58
|
before do
|
61
59
|
@uploader.aliyun_private_read = true
|
62
|
-
@
|
60
|
+
@bucket = CarrierWave::Aliyun::Bucket.new(@uploader)
|
63
61
|
end
|
64
62
|
|
65
63
|
it 'should get url include token' do
|
66
|
-
url = @
|
64
|
+
url = @bucket.private_get_url('bar/foo.jpg')
|
67
65
|
# http://oss-cn-beijing.aliyuncs.com.//carrierwave-aliyun-test.oss-cn-beijing.aliyuncs.com/bar/foo.jpg?OSSAccessKeyId=1OpWEtPTjIDv5u8q&Expires=1455172009&Signature=4ibgQpfHOjVpqxG6162S8Ar3c6c=
|
68
66
|
expect(url).to include(*%w(Signature Expires OSSAccessKeyId))
|
69
67
|
expect(url).to include "http://#{@uploader.aliyun_bucket}.oss-#{@uploader.aliyun_area}.aliyuncs.com/bar/foo.jpg"
|
70
68
|
end
|
71
69
|
|
72
|
-
it
|
73
|
-
url = @
|
70
|
+
it 'should get url with :thumb' do
|
71
|
+
url = @bucket.private_get_url('bar/foo.jpg', thumb: '@100w_200h_90q')
|
74
72
|
expect(url).to include "http://#{@uploader.aliyun_bucket}.img-cn-beijing.aliyuncs.com/bar/foo.jpg@100w_200h_90q"
|
75
73
|
end
|
76
74
|
end
|
77
75
|
|
78
76
|
describe 'File' do
|
79
77
|
it 'should have respond_to identifier' do
|
80
|
-
f = CarrierWave::Storage::
|
78
|
+
f = CarrierWave::Storage::AliyunFile.new(@uploader, '', '')
|
81
79
|
expect(f).to respond_to(:identifier)
|
82
80
|
expect(f).to respond_to(:filename)
|
83
81
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,28 +2,30 @@ require 'rubygems'
|
|
2
2
|
require 'rspec'
|
3
3
|
require 'rails'
|
4
4
|
require 'active_record'
|
5
|
-
require
|
5
|
+
require 'carrierwave'
|
6
6
|
require 'carrierwave/orm/activerecord'
|
7
7
|
require 'carrierwave/processing/mini_magick'
|
8
|
+
require 'open-uri'
|
9
|
+
require 'net/http'
|
8
10
|
|
9
11
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
12
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
13
|
|
12
|
-
require
|
13
|
-
|
14
|
+
require 'carrierwave-aliyun'
|
14
15
|
|
15
16
|
module Rails
|
16
17
|
class <<self
|
17
18
|
def root
|
18
|
-
[File.expand_path(__FILE__).split('/')[0..-3].join('/'),
|
19
|
+
[File.expand_path(__FILE__).split('/')[0..-3].join('/'), 'spec'].join('/')
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
ActiveRecord::Migration.verbose = false
|
24
25
|
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
26
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
|
27
|
+
database: ':memory:')
|
25
28
|
|
26
|
-
# 测试的时候需要修改这个地方
|
27
29
|
ALIYUN_ACCESS_ID = ENV['ALIYUN_ACCESS_ID'] || ''
|
28
30
|
ALIYUN_ACCESS_KEY = ENV['ALIYUN_ACCESS_KEY'] || ''
|
29
31
|
ALIYUN_BUCKET = ENV['ALIYUN_BUCKET'] || 'carrierwave-aliyun-test'
|
@@ -39,5 +41,5 @@ CarrierWave.configure do |config|
|
|
39
41
|
end
|
40
42
|
|
41
43
|
def load_file(fname)
|
42
|
-
File.open([Rails.root,fname].join(
|
44
|
+
File.open([Rails.root, fname].join('/'))
|
43
45
|
end
|
data/spec/upload_spec.rb
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
require "net/http"
|
5
|
-
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
6
|
-
|
7
|
-
describe "Upload" do
|
3
|
+
describe 'Upload' do
|
8
4
|
def setup_db
|
9
|
-
ActiveRecord::Schema.define(:
|
5
|
+
ActiveRecord::Schema.define(version: 1) do
|
10
6
|
create_table :photos do |t|
|
11
7
|
t.column :image, :string
|
12
8
|
t.column :content_type, :string
|
@@ -29,11 +25,11 @@ describe "Upload" do
|
|
29
25
|
include CarrierWave::MiniMagick
|
30
26
|
|
31
27
|
version :small do
|
32
|
-
process :
|
28
|
+
process resize_to_fill: [120, 120]
|
33
29
|
end
|
34
30
|
|
35
31
|
def store_dir
|
36
|
-
|
32
|
+
'photos'
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
@@ -41,7 +37,11 @@ describe "Upload" do
|
|
41
37
|
include CarrierWave::MiniMagick
|
42
38
|
|
43
39
|
def store_dir
|
44
|
-
|
40
|
+
'attachs'
|
41
|
+
end
|
42
|
+
|
43
|
+
def content_disposition
|
44
|
+
"attachment;filename=#{file.original_filename}"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -53,7 +53,6 @@ describe "Upload" do
|
|
53
53
|
mount_uploader :file, AttachUploader
|
54
54
|
end
|
55
55
|
|
56
|
-
|
57
56
|
before :all do
|
58
57
|
setup_db
|
59
58
|
end
|
@@ -62,35 +61,39 @@ describe "Upload" do
|
|
62
61
|
drop_db
|
63
62
|
end
|
64
63
|
|
65
|
-
describe
|
66
|
-
context
|
64
|
+
describe 'Upload Image' do
|
65
|
+
context 'should upload image' do
|
67
66
|
before(:all) do
|
68
|
-
@file = load_file(
|
69
|
-
@file1 = load_file(
|
70
|
-
@photo = Photo.new(:
|
71
|
-
@photo1 = Photo.new(:
|
67
|
+
@file = load_file('foo.jpg')
|
68
|
+
@file1 = load_file('foo.gif')
|
69
|
+
@photo = Photo.new(image: @file)
|
70
|
+
@photo1 = Photo.new(image: @file1)
|
72
71
|
end
|
73
72
|
|
74
|
-
it
|
73
|
+
it 'should upload file' do
|
75
74
|
expect(@photo.save).to eq true
|
76
|
-
expect(@
|
75
|
+
expect(@photo[:image].present?).to eq true
|
76
|
+
# FIXME: image? 需要实现
|
77
|
+
# expect(@photo.image?).to eq true
|
77
78
|
end
|
78
79
|
|
79
|
-
it
|
80
|
+
it 'should get uploaded file' do
|
80
81
|
img = open(@photo.image.url)
|
81
82
|
expect(img.size).to eq @file.size
|
82
83
|
expect(img.content_type).to eq 'image/jpeg'
|
84
|
+
|
85
|
+
expect(@photo1.save).to eq true
|
83
86
|
img1 = open(@photo1.image.url)
|
84
87
|
expect(img1.size).to eq @file1.size
|
85
88
|
expect(img1.content_type).to eq 'image/gif'
|
86
89
|
end
|
87
90
|
|
88
|
-
it
|
91
|
+
it 'sholud get small version uploaded file' do
|
89
92
|
expect(open(@photo.image.small.url)).not_to eq nil
|
90
93
|
expect(open(@photo1.image.small.url)).not_to eq nil
|
91
94
|
end
|
92
95
|
|
93
|
-
it
|
96
|
+
it 'should get Aliyun OSS thumb url with :thumb option' do
|
94
97
|
url = @photo.image.url(thumb: '@150w_140h.png')
|
95
98
|
expect(url).to include('.img-')
|
96
99
|
expect(url).to include('@150w_140h.png')
|
@@ -103,28 +106,29 @@ describe "Upload" do
|
|
103
106
|
end
|
104
107
|
end
|
105
108
|
|
106
|
-
context
|
109
|
+
context 'should update zip' do
|
107
110
|
before(:all) do
|
108
|
-
@file = load_file(
|
109
|
-
@attachment = Attachment.new(:
|
111
|
+
@file = load_file('foo.zip')
|
112
|
+
@attachment = Attachment.new(file: @file)
|
110
113
|
end
|
111
114
|
|
112
|
-
it
|
115
|
+
it 'should upload file' do
|
113
116
|
expect(@attachment.save).to eq true
|
114
117
|
end
|
115
118
|
|
116
|
-
it
|
119
|
+
it 'should get uploaded file' do
|
117
120
|
attach = open(@attachment.file.url)
|
118
121
|
expect(attach.size).to eq @file.size
|
119
122
|
expect(attach.content_type).to eq 'application/zip'
|
123
|
+
expect(attach.meta['content-disposition']).to eq 'attachment;filename=foo.zip'
|
120
124
|
end
|
121
125
|
|
122
|
-
it
|
126
|
+
it 'should delete old file when upload a new file again' do
|
123
127
|
old_url = @attachment.file.url
|
124
|
-
@attachment.file = load_file(
|
128
|
+
@attachment.file = load_file('foo.gif')
|
125
129
|
@attachment.save
|
126
130
|
res = Net::HTTP.get_response(URI.parse(old_url))
|
127
|
-
expect(res.code).to eq
|
131
|
+
expect(res.code).to eq '404'
|
128
132
|
end
|
129
133
|
end
|
130
134
|
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.7.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: 2016-
|
11
|
+
date: 2016-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
49
|
- ".rspec"
|
50
|
+
- ".rubocop.yml"
|
50
51
|
- ".travis.yml"
|
51
52
|
- CHANGELOG.md
|
52
53
|
- Gemfile
|
@@ -55,9 +56,11 @@ files:
|
|
55
56
|
- Rakefile
|
56
57
|
- carrierwave-aliyun.gemspec
|
57
58
|
- lib/carrierwave-aliyun.rb
|
59
|
+
- lib/carrierwave/aliyun/bucket.rb
|
58
60
|
- lib/carrierwave/aliyun/configuration.rb
|
59
61
|
- lib/carrierwave/aliyun/version.rb
|
60
62
|
- lib/carrierwave/storage/aliyun.rb
|
63
|
+
- lib/carrierwave/storage/aliyun_file.rb
|
61
64
|
- spec/aliyun_spec.rb
|
62
65
|
- spec/foo.gif
|
63
66
|
- spec/foo.jpg
|