carrierwave-aliyun 1.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/README.md +1 -1
- data/lib/carrierwave/aliyun/bucket.rb +43 -42
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +3 -14
- data/lib/carrierwave/storage/aliyun_file.rb +25 -11
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09185a0291f96fed424df86a4be3815f13171b74f4b6d891b945de90bdc3924d'
|
4
|
+
data.tar.gz: ff50669bd39b9da0ff3b9c5c00c9e646fc0f3f46e7f0b1b45643c9d76abe66fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd28e434c5bd6d43ccfff8f5b98dff4343d1440c3ded461397b95bde45e280bfde4481e0c06d0af3da8d38009620546c36cd777fd5c6bae893038e94533cc8ba
|
7
|
+
data.tar.gz: 88a37bc5bbf656be44cb50232943faac2ba26698fdee306f3441f0365e853066fdfb97297b9c7c0012d256ba80f02b69f78af8e585938de7917926dfa473f4a1
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
## 1.2.1
|
2
|
+
|
3
|
+
- 修正 #79 某些场景中文文件名无法正确上传的问题;
|
4
|
+
- 修正 #70 上传文件以后 tmp 目录依然还残留着 cache 文件的问题;
|
5
|
+
|
6
|
+
## 1.2.1
|
7
|
+
|
8
|
+
- 实现 `size` 方法,修正更新图片数组时,新上传的图片会取代旧图片的问题。(#73)
|
9
|
+
|
10
|
+
## 1.2.0
|
11
|
+
|
12
|
+
- 整理修复 OSS 文件访问 URL 的生成方式,去掉 img host,保持和最新 OSS API 一样的逻辑。
|
13
|
+
- 生成 URL 的时候,不再强制替换为 https,保持 `aliyun_host` 的配置;
|
14
|
+
|
15
|
+
## 1.1.2
|
16
|
+
|
17
|
+
- 修正 aliyun-sdk 0.7.0 以上 Thumb URL 生成的支持;
|
18
|
+
- Requirement aliyun-sdk >= 0.7.0;
|
19
|
+
|
20
|
+
## 1.1.2
|
21
|
+
|
22
|
+
- 修正废弃调用方式,避免在 Ruby 2.7 里面出现 warning.
|
23
|
+
|
1
24
|
## 1.1.1
|
2
25
|
|
3
26
|
- 对于 CarrierWave 的 cache 机制正确支持;
|
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
|
-
[](https://rubygems.org/gems/carrierwave-aliyun) [](https://rubygems.org/gems/carrierwave-aliyun) [](https://github.com/huacnlee/carrierwave-aliyun/actions?query=workflow%3Abuild)
|
6
6
|
|
7
7
|
> NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用,如果你需要直接用 Aliyun OSS,可以尝试用 [aliyun-sdk](https://github.com/aliyun/aliyun-oss-ruby-sdk) 这个 Gem。
|
8
8
|
|
@@ -3,12 +3,11 @@
|
|
3
3
|
module CarrierWave
|
4
4
|
module Aliyun
|
5
5
|
class Bucket
|
6
|
-
PATH_PREFIX = %r{^/}
|
6
|
+
PATH_PREFIX = %r{^/}.freeze
|
7
7
|
CHUNK_SIZE = 1024 * 1024
|
8
8
|
|
9
|
-
attr_reader :access_key_id, :access_key_secret, :bucket, :region, :mode, :host
|
10
|
-
|
11
|
-
attr_reader :endpoint, :img_endpoint, :upload_endpoint
|
9
|
+
attr_reader :access_key_id, :access_key_secret, :bucket, :region, :mode, :host, :endpoint, :upload_endpoint,
|
10
|
+
:get_endpoint
|
12
11
|
|
13
12
|
def initialize(uploader)
|
14
13
|
if uploader.aliyun_area.present?
|
@@ -16,7 +15,7 @@ module CarrierWave
|
|
16
15
|
uploader.aliyun_region ||= uploader.aliyun_area
|
17
16
|
end
|
18
17
|
|
19
|
-
|
18
|
+
unless uploader.aliyun_private_read.nil?
|
20
19
|
ActiveSupport::Deprecation.warn(%(config.aliyun_private_read will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_mode = :private` instead.))
|
21
20
|
uploader.aliyun_mode ||= uploader.aliyun_private_read ? :private : :public
|
22
21
|
end
|
@@ -38,15 +37,15 @@ module CarrierWave
|
|
38
37
|
@mode = (uploader.aliyun_mode || :public).to_sym
|
39
38
|
|
40
39
|
# Host for get request
|
41
|
-
@
|
40
|
+
@endpoint = "https://#{bucket}.oss-#{region}.aliyuncs.com"
|
41
|
+
@host = uploader.aliyun_host || @endpoint
|
42
42
|
|
43
43
|
unless @host.include?("//")
|
44
|
-
raise "config.aliyun_host requirement include // http:// or https://, but you give: #{
|
44
|
+
raise "config.aliyun_host requirement include // http:// or https://, but you give: #{host}"
|
45
45
|
end
|
46
46
|
|
47
|
-
@
|
48
|
-
@upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{
|
49
|
-
@img_endpoint = "https://img-#{self.region}.aliyuncs.com"
|
47
|
+
@get_endpoint = "https://oss-#{region}.aliyuncs.com"
|
48
|
+
@upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{region}-internal.aliyuncs.com" : "https://oss-#{region}.aliyuncs.com"
|
50
49
|
end
|
51
50
|
|
52
51
|
# 上传文件
|
@@ -70,7 +69,7 @@ module CarrierWave
|
|
70
69
|
stream << file.read(CHUNK_SIZE) until file.eof?
|
71
70
|
end
|
72
71
|
path_to_url(path)
|
73
|
-
rescue => e
|
72
|
+
rescue StandardError => e
|
74
73
|
raise "Put file failed: #{e}"
|
75
74
|
end
|
76
75
|
end
|
@@ -95,7 +94,7 @@ module CarrierWave
|
|
95
94
|
end
|
96
95
|
|
97
96
|
[obj, chunk_buff.join("")]
|
98
|
-
rescue => e
|
97
|
+
rescue StandardError => e
|
99
98
|
raise "Get content faild: #{e}"
|
100
99
|
end
|
101
100
|
|
@@ -110,34 +109,36 @@ module CarrierWave
|
|
110
109
|
path = path.sub(PATH_PREFIX, "")
|
111
110
|
oss_upload_client.delete_object(path)
|
112
111
|
path_to_url(path)
|
113
|
-
rescue => e
|
112
|
+
rescue StandardError => e
|
114
113
|
raise "Delete failed: #{e}"
|
115
114
|
end
|
116
115
|
|
117
116
|
##
|
118
117
|
# 根据配置返回完整的上传文件的访问地址
|
119
118
|
def path_to_url(path, thumb: nil)
|
120
|
-
path
|
121
|
-
|
122
|
-
if thumb
|
123
|
-
[self.host, [path, thumb].join("")].join("/")
|
124
|
-
else
|
125
|
-
[self.host, path].join("/")
|
126
|
-
end
|
119
|
+
get_url(path, thumb: thumb)
|
127
120
|
end
|
128
121
|
|
129
122
|
# 私有空间访问地址,会带上实时算出的 token 信息
|
130
123
|
# 有效期 15 minutes
|
131
124
|
def private_get_url(path, thumb: nil)
|
125
|
+
get_url(path, private: true, thumb: thumb)
|
126
|
+
end
|
127
|
+
|
128
|
+
def get_url(path, private: false, thumb: nil)
|
132
129
|
path = path.sub(PATH_PREFIX, "")
|
133
130
|
|
134
|
-
url = if thumb
|
135
|
-
|
131
|
+
url = if thumb&.start_with?("?")
|
132
|
+
# foo.jpg?x-oss-process=image/resize,h_100
|
133
|
+
parameters = { "x-oss-process" => thumb.split("=").last }
|
134
|
+
oss_client.object_url(path, private, 15.minutes, parameters)
|
136
135
|
else
|
137
|
-
oss_client.object_url(path,
|
136
|
+
oss_client.object_url(path, private, 15.minutes)
|
138
137
|
end
|
139
138
|
|
140
|
-
url.
|
139
|
+
url = [url, thumb].join("") if !private && !thumb&.start_with?("?")
|
140
|
+
|
141
|
+
url.sub(endpoint, host)
|
141
142
|
end
|
142
143
|
|
143
144
|
def head(path)
|
@@ -145,28 +146,28 @@ module CarrierWave
|
|
145
146
|
oss_upload_client.get_object(path)
|
146
147
|
end
|
147
148
|
|
149
|
+
# list_objects for test
|
150
|
+
def list_objects(opts = {})
|
151
|
+
oss_client.list_objects(opts)
|
152
|
+
end
|
153
|
+
|
148
154
|
private
|
149
155
|
|
150
|
-
|
151
|
-
|
152
|
-
client = ::Aliyun::OSS::Client.new(endpoint: self.endpoint,
|
153
|
-
access_key_id: self.access_key_id, access_key_secret: self.access_key_secret)
|
154
|
-
@oss_client = client.get_bucket(self.bucket)
|
155
|
-
end
|
156
|
+
def oss_client
|
157
|
+
return @oss_client if defined?(@oss_client)
|
156
158
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
@img_client = client.get_bucket(self.bucket)
|
162
|
-
end
|
159
|
+
client = ::Aliyun::OSS::Client.new(endpoint: get_endpoint, access_key_id: access_key_id,
|
160
|
+
access_key_secret: access_key_secret)
|
161
|
+
@oss_client = client.get_bucket(bucket)
|
162
|
+
end
|
163
163
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
164
|
+
def oss_upload_client
|
165
|
+
return @oss_upload_client if defined?(@oss_upload_client)
|
166
|
+
|
167
|
+
client = ::Aliyun::OSS::Client.new(endpoint: upload_endpoint, access_key_id: access_key_id,
|
168
|
+
access_key_secret: access_key_secret)
|
169
|
+
@oss_upload_client = client.get_bucket(bucket)
|
170
|
+
end
|
170
171
|
end
|
171
172
|
end
|
172
173
|
end
|
@@ -37,22 +37,11 @@ module CarrierWave
|
|
37
37
|
# do nothing, because there's no such things as 'empty directory'
|
38
38
|
end
|
39
39
|
|
40
|
-
def clean_cache!(_seconds)
|
41
|
-
will_remove_keys = []
|
42
|
-
bucket.list_objects(prefix: uploader.cache_path).each do |file|
|
43
|
-
next unless file.is_a?(Object)
|
44
|
-
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
|
45
|
-
time = Time.at(*time)
|
46
|
-
will_remove_keys << item.key if time < (Time.now.utc - seconds)
|
47
|
-
end
|
48
|
-
bucket.batch_delete_objects(will_remove_keys)
|
49
|
-
end
|
50
|
-
|
51
40
|
private
|
52
41
|
|
53
|
-
|
54
|
-
|
55
|
-
|
42
|
+
def bucket
|
43
|
+
@bucket ||= CarrierWave::Aliyun::Bucket.new(uploader)
|
44
|
+
end
|
56
45
|
end
|
57
46
|
end
|
58
47
|
end
|
@@ -3,13 +3,26 @@
|
|
3
3
|
module CarrierWave
|
4
4
|
module Storage
|
5
5
|
class AliyunFile
|
6
|
+
attr_writer :file
|
6
7
|
attr_reader :uploader, :path
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
alias filename path
|
10
|
+
alias identifier filename
|
10
11
|
|
11
12
|
def initialize(uploader, base, path)
|
12
|
-
@uploader
|
13
|
+
@uploader = uploader
|
14
|
+
@path = path
|
15
|
+
@base = base
|
16
|
+
end
|
17
|
+
|
18
|
+
def file
|
19
|
+
@file ||= bucket.get(path).try(:first)
|
20
|
+
end
|
21
|
+
|
22
|
+
def size
|
23
|
+
file.headers[:content_length].to_i
|
24
|
+
rescue StandardError
|
25
|
+
nil
|
13
26
|
end
|
14
27
|
|
15
28
|
def read
|
@@ -21,7 +34,7 @@ module CarrierWave
|
|
21
34
|
def delete
|
22
35
|
bucket.delete(path)
|
23
36
|
true
|
24
|
-
rescue => e
|
37
|
+
rescue StandardError => e
|
25
38
|
# If the file's not there, don't panic
|
26
39
|
puts "carrierwave-aliyun delete file failed: #{e}"
|
27
40
|
nil
|
@@ -34,9 +47,9 @@ module CarrierWave
|
|
34
47
|
#
|
35
48
|
def url(opts = {})
|
36
49
|
if bucket.mode == :private
|
37
|
-
bucket.private_get_url(path, opts)
|
50
|
+
bucket.private_get_url(path, **opts)
|
38
51
|
else
|
39
|
-
bucket.path_to_url(path, opts)
|
52
|
+
bucket.path_to_url(path, **opts)
|
40
53
|
end
|
41
54
|
end
|
42
55
|
|
@@ -53,7 +66,7 @@ module CarrierWave
|
|
53
66
|
new_file.copy_to(path)
|
54
67
|
else
|
55
68
|
fog_file = new_file.to_file
|
56
|
-
bucket.put(path, fog_file, headers)
|
69
|
+
bucket.put(path, fog_file, **headers)
|
57
70
|
fog_file.close if fog_file && !fog_file.closed?
|
58
71
|
end
|
59
72
|
true
|
@@ -82,7 +95,8 @@ module CarrierWave
|
|
82
95
|
|
83
96
|
def original_filename
|
84
97
|
return @original_filename if @original_filename
|
85
|
-
|
98
|
+
|
99
|
+
if @file&.respond_to?(:original_filename)
|
86
100
|
@file.original_filename
|
87
101
|
elsif path
|
88
102
|
::File.basename(path)
|
@@ -91,9 +105,9 @@ module CarrierWave
|
|
91
105
|
|
92
106
|
private
|
93
107
|
|
94
|
-
|
95
|
-
|
96
|
-
|
108
|
+
def bucket
|
109
|
+
@bucket ||= CarrierWave::Aliyun::Bucket.new(uploader)
|
110
|
+
end
|
97
111
|
end
|
98
112
|
end
|
99
113
|
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: 1.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.7.0
|
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: 0.
|
40
|
+
version: 0.7.0
|
41
41
|
description: Aliyun OSS support for Carrierwave
|
42
42
|
email:
|
43
43
|
- huacnlee@gmail.com
|
@@ -57,7 +57,7 @@ homepage: https://github.com/huacnlee/carrierwave-aliyun
|
|
57
57
|
licenses:
|
58
58
|
- MIT
|
59
59
|
metadata: {}
|
60
|
-
post_install_message:
|
60
|
+
post_install_message:
|
61
61
|
rdoc_options: []
|
62
62
|
require_paths:
|
63
63
|
- lib
|
@@ -72,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
|
-
rubygems_version: 3.
|
76
|
-
signing_key:
|
75
|
+
rubygems_version: 3.2.3
|
76
|
+
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Aliyun OSS support for Carrierwave
|
79
79
|
test_files: []
|