carrierwave-aliyun 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2566558b43cc5a630c144191041c2a623249734b2875e1da28f8778a553adf2b
4
- data.tar.gz: 3f773081c2e49cd85b83b0677afc108694a1b9b604685460e815330f736b81dc
3
+ metadata.gz: 54c88d264e49631dd46a637080fc79b10cb73d487910a1608afd4ac67cf65561
4
+ data.tar.gz: 7362e2b9d48547d369994443b105af5f4d57f7a34b64a7c78b51b9ecf940aa8e
5
5
  SHA512:
6
- metadata.gz: c51793690e631ceb49e0536f76471a4d34bf4cbbb8adeabfddfed95abd4d924c20f1c2c8ac017580bc8785c00c4f0ef1e3b30ca534ecc333aaeb20040fbfe38f
7
- data.tar.gz: 34381bab3c7e15f45a231a37adba1481c75499083cdf69a408b0891bb2915ba517b4333926147760c2ee250d9a9675233e2f804068cd6f884f6d145ca71762de
6
+ metadata.gz: 6589f76ad4cc466e3bd1acb690a0759786cb7ec2517fe2e51c2aeb0ad044053ff1b6d373bc04b51c75db6cb34551f0fe1a9d22c688bb66579a1d8f8303af37c7
7
+ data.tar.gz: '00487944ad9d21be007c4f1148ce676e376c621f9d1de6207347d1f621351fda81c48b2f8100801cb6cff18965923447ae1034a396406da93b012280bb34590e'
@@ -1,3 +1,8 @@
1
+ ## 1.2.0
2
+
3
+ - 整理修复 OSS 文件访问 URL 的生成方式,去掉 img host,保持和最新 OSS API 一样的逻辑。
4
+ - 生成 URL 的时候,不再强制替换为 https,保持 `aliyun_host` 的配置;
5
+
1
6
  ## 1.1.2
2
7
 
3
8
  - 修正 aliyun-sdk 0.7.0 以上 Thumb URL 生成的支持;
@@ -3,12 +3,12 @@
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
9
  attr_reader :access_key_id, :access_key_secret, :bucket, :region, :mode, :host
10
10
 
11
- attr_reader :endpoint, :img_endpoint, :upload_endpoint
11
+ attr_reader :endpoint, :upload_endpoint, :get_endpoint
12
12
 
13
13
  def initialize(uploader)
14
14
  if uploader.aliyun_area.present?
@@ -16,7 +16,7 @@ module CarrierWave
16
16
  uploader.aliyun_region ||= uploader.aliyun_area
17
17
  end
18
18
 
19
- if uploader.aliyun_private_read != nil
19
+ unless uploader.aliyun_private_read.nil?
20
20
  ActiveSupport::Deprecation.warn(%(config.aliyun_private_read will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_mode = :private` instead.))
21
21
  uploader.aliyun_mode ||= uploader.aliyun_private_read ? :private : :public
22
22
  end
@@ -38,15 +38,15 @@ module CarrierWave
38
38
  @mode = (uploader.aliyun_mode || :public).to_sym
39
39
 
40
40
  # Host for get request
41
- @host = uploader.aliyun_host || "https://#{self.bucket}.oss-#{self.region}.aliyuncs.com"
41
+ @endpoint = "https://#{bucket}.oss-#{region}.aliyuncs.com"
42
+ @host = uploader.aliyun_host || @endpoint
42
43
 
43
44
  unless @host.include?("//")
44
- raise "config.aliyun_host requirement include // http:// or https://, but you give: #{self.host}"
45
+ raise "config.aliyun_host requirement include // http:// or https://, but you give: #{host}"
45
46
  end
46
47
 
47
- @endpoint = "https://oss-#{self.region}.aliyuncs.com"
48
- @upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{self.region}-internal.aliyuncs.com" : @endpoint
49
- @img_endpoint = "https://img-#{self.region}.aliyuncs.com"
48
+ @get_endpoint = "https://oss-#{region}.aliyuncs.com"
49
+ @upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{region}-internal.aliyuncs.com" : "https://oss-#{region}.aliyuncs.com"
50
50
  end
51
51
 
52
52
  # 上传文件
@@ -70,7 +70,7 @@ module CarrierWave
70
70
  stream << file.read(CHUNK_SIZE) until file.eof?
71
71
  end
72
72
  path_to_url(path)
73
- rescue => e
73
+ rescue StandardError => e
74
74
  raise "Put file failed: #{e}"
75
75
  end
76
76
  end
@@ -95,7 +95,7 @@ module CarrierWave
95
95
  end
96
96
 
97
97
  [obj, chunk_buff.join("")]
98
- rescue => e
98
+ rescue StandardError => e
99
99
  raise "Get content faild: #{e}"
100
100
  end
101
101
 
@@ -110,36 +110,38 @@ module CarrierWave
110
110
  path = path.sub(PATH_PREFIX, "")
111
111
  oss_upload_client.delete_object(path)
112
112
  path_to_url(path)
113
- rescue => e
113
+ rescue StandardError => e
114
114
  raise "Delete failed: #{e}"
115
115
  end
116
116
 
117
117
  ##
118
118
  # 根据配置返回完整的上传文件的访问地址
119
119
  def path_to_url(path, thumb: nil)
120
- path = path.sub(PATH_PREFIX, "")
121
-
122
- if thumb
123
- [self.host, [path, thumb].join("")].join("/")
124
- else
125
- [self.host, path].join("/")
126
- end
120
+ get_url(path, thumb: thumb)
127
121
  end
128
122
 
129
123
  # 私有空间访问地址,会带上实时算出的 token 信息
130
124
  # 有效期 15 minutes
131
125
  def private_get_url(path, thumb: nil)
126
+ get_url(path, private: true, thumb: thumb)
127
+ end
128
+
129
+ def get_url(path, private: false, thumb: nil)
132
130
  path = path.sub(PATH_PREFIX, "")
133
131
 
134
- url = if thumb
135
- thumb = thumb.gsub('?', '')
136
- parameters = Hash[*thumb.split('=')]
137
- img_client.object_url(path, true, 15.minutes, parameters)
132
+ url = if thumb&.start_with?("?")
133
+ # foo.jpg?x-oss-process=image/resize,h_100
134
+ parameters = { "x-oss-process" => thumb.split("=").last }
135
+ oss_client.object_url(path, private, 15.minutes, parameters)
138
136
  else
139
- oss_client.object_url(path, true, 15.minutes)
137
+ oss_client.object_url(path, private, 15.minutes)
140
138
  end
141
139
 
142
- url.sub("http://", "https://")
140
+ unless private
141
+ url = [url, thumb].join("") unless thumb&.start_with?("?")
142
+ end
143
+
144
+ url.sub(endpoint, host)
143
145
  end
144
146
 
145
147
  def head(path)
@@ -149,26 +151,19 @@ module CarrierWave
149
151
 
150
152
  private
151
153
 
152
- def oss_client
153
- return @oss_client if defined?(@oss_client)
154
- client = ::Aliyun::OSS::Client.new(endpoint: self.endpoint,
155
- access_key_id: self.access_key_id, access_key_secret: self.access_key_secret)
156
- @oss_client = client.get_bucket(self.bucket)
157
- end
154
+ def oss_client
155
+ return @oss_client if defined?(@oss_client)
158
156
 
159
- def img_client
160
- return @img_client if defined?(@img_client)
161
- client = ::Aliyun::OSS::Client.new(endpoint: self.img_endpoint,
162
- access_key_id: self.access_key_id, access_key_secret: self.access_key_secret)
163
- @img_client = client.get_bucket(self.bucket)
164
- end
157
+ client = ::Aliyun::OSS::Client.new(endpoint: get_endpoint, access_key_id: access_key_id, access_key_secret: access_key_secret)
158
+ @oss_client = client.get_bucket(bucket)
159
+ end
165
160
 
166
- def oss_upload_client
167
- return @oss_upload_client if defined?(@oss_upload_client)
168
- client = ::Aliyun::OSS::Client.new(endpoint: self.upload_endpoint,
169
- access_key_id: self.access_key_id, access_key_secret: self.access_key_secret)
170
- @oss_upload_client = client.get_bucket(self.bucket)
171
- end
161
+ def oss_upload_client
162
+ return @oss_upload_client if defined?(@oss_upload_client)
163
+
164
+ client = ::Aliyun::OSS::Client.new(endpoint: upload_endpoint, access_key_id: access_key_id, access_key_secret: access_key_secret)
165
+ @oss_upload_client = client.get_bucket(bucket)
166
+ end
172
167
  end
173
168
  end
174
169
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CarrierWave
4
4
  module Aliyun
5
- VERSION = "1.1.3"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  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.1.3
4
+ version: 1.2.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: 2020-03-09 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave