activestorage-aliyun 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: bfac27721ec3ce8613444b343030117a4ddf6d28
4
- data.tar.gz: 7ed58307e3dec06209a29436127d5f4f188dae0f
3
+ metadata.gz: 796ec942f06897cf76873d1b6dadeb23a565345f
4
+ data.tar.gz: fd06616edba762ac6ccf4e352cbec7a17fc220dd
5
5
  SHA512:
6
- metadata.gz: 7dfb3e7aa65baa48d1040fcb8b11f867b1fab78a0eedb21b7ce1ca7ab37d05b0e56521c61fc5ca195f2f72adeaf6ed66b346bcca0349e9b9bf7ec1a2c7c27f46
7
- data.tar.gz: dcf684e6ff9ffce4ef3e75ec5f7db26513cad9cf8ce45de3a68a501fd048a95365ab396185d6e6d28868d7e53f66e0f904d3676e10e69e969d4d68baf5b1e617
6
+ metadata.gz: 91698652328b356a3df0c7fbd1fa45a4f5e7185df39d097d19402fdf77e3eaad9190af997c4b21ed89dac385e87c9cebf27ef6303ca222a912291c8feec77f1b
7
+ data.tar.gz: d935a34961783cd60f41055480557a14017a01b3db3b60194c39a2801f8907c38ed403cb578b6244803070e30f1c037cbbc85d8330421ea61bff2e40049aff69
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.0
2
+
3
+ - Add `mode` config for setup OSS ACL, `mode: "private"` will always output URL that have signature info.
4
+ - Support `disposition: :attachment` option for `service_url` method for download original filename.
5
+
1
6
  ## 0.2.0
2
7
 
3
8
  - Add url_for_direct_upload support.
data/README.md CHANGED
@@ -33,13 +33,29 @@ production:
33
33
  endpoint: "https://oss-cn-beijing.aliyuncs.com"
34
34
  # path prefix, default: /
35
35
  path: "my-app-files"
36
+ # Bucket mode: [public, private], default: public
37
+ mode: "public"
36
38
  ```
37
39
 
38
- Use image:
40
+ ### Use for image url
39
41
 
40
42
  ```erb
41
- - Orignial File URL: <%= image_tag @photo.image.service_url %>
42
- - Thumb with OSS image service: <%= image_tag @photo.image.service_url(filename: 'x-oss-process=image/resize,h_100,w_100') %>
43
+ Orignial File URL:
44
+
45
+ <%= image_tag @photo.image.service_url %>
46
+
47
+ Thumb with OSS image service:
48
+
49
+ <%= image_tag @photo.image.service_url(filename: 'x-oss-process=image/resize,h_100,w_100') %>
50
+ ```
51
+
52
+ ### Use for file download
53
+
54
+ If you want to get original filename (Include Chinese and other UTF-8 chars), for example: `演示文件 download.zip`, you need present `disposition: :attachment` option.
55
+
56
+ ```erb
57
+ #
58
+ <%= image_tag @photo.image.service_url(disposition: :attachment) %>
43
59
  ```
44
60
 
45
61
  ## Contributing
@@ -54,14 +54,21 @@ module ActiveStorage
54
54
 
55
55
  def url(key, expires_in:, filename:, content_type:, disposition:)
56
56
  instrument :url, key: key do |payload|
57
- generated_url = bucket.object_url(path_for(key), false, expires_in)
58
- generated_url.gsub('http://', 'https://')
59
- if filename.to_s.include?("x-oss-process")
60
- generated_url = [generated_url, filename].join("?")
57
+ sign = private_mode? || disposition == :attachment
58
+ filekey = path_for(key)
59
+ params = {}
60
+
61
+ if disposition == :attachment
62
+ params["response-content-type"] = content_type if content_type
63
+ params["response-content-disposition"] = "attachment;filename*=UTF-8''#{CGI.escape(filename.to_s)}"
64
+ else
65
+ if filename.to_s.include?("x-oss-process")
66
+ params["x-oss-process"] = filename.to_s.split("=").last
67
+ end
61
68
  end
62
69
 
70
+ generated_url = object_url(filekey, sign: sign, expires_in: expires_in, params: params)
63
71
  payload[:url] = generated_url
64
-
65
72
  generated_url
66
73
  end
67
74
  end
@@ -97,21 +104,51 @@ module ActiveStorage
97
104
  attr_reader :config
98
105
 
99
106
  def path_for(key)
100
- return key if !config.fetch(:path, nil)
107
+ return key unless config.fetch(:path, nil)
101
108
  File.join(config.fetch(:path), key)
102
109
  end
103
110
 
111
+ def object_url(key, sign:, expires_in: 60, params: {})
112
+ url = bucket.object_url(key, false)
113
+ unless sign
114
+ return url if params.blank?
115
+ return url + "?" + params.to_query
116
+ end
117
+
118
+ resource = "/#{bucket.name}/#{key}"
119
+ expires = Time.now.to_i + expires_in
120
+ query = {
121
+ "Expires" => expires,
122
+ "OSSAccessKeyId" => config.fetch(:access_key_id)
123
+ }
124
+ query.merge!(params)
125
+
126
+ if params.present?
127
+ resource += "?" + params.map { |k, v| "#{k}=#{v}" }.sort.join("&")
128
+ end
129
+
130
+ string_to_sign = ["GET", "", "", expires, resource].join("\n")
131
+ query["Signature"] = bucket.sign(string_to_sign)
132
+
133
+ [url, query.to_query].join('?')
134
+ end
135
+
104
136
  def bucket
105
137
  return @bucket if defined? @bucket
106
138
  @bucket = client.get_bucket(config.fetch(:bucket))
107
139
  @bucket
108
140
  end
109
141
 
142
+ # Bucket mode :public | :private
143
+ def private_mode?
144
+ @private_mode ||= config.fetch(:mode, "public").to_s == "private"
145
+ end
146
+
110
147
  def authorization(key, content_type, checksum, date)
111
148
  filename = File.expand_path("/#{bucket.name}/#{path_for(key)}")
112
- addition_headers = "x-oss-date:"+date
149
+ addition_headers = "x-oss-date:#{date}"
113
150
  sign = ["PUT", checksum, content_type, date, addition_headers, filename].join("\n")
114
- signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', config.fetch(:access_key_secret), sign))
151
+ signature = bucket.sign(sign)
115
152
  "OSS " + config.fetch(:access_key_id) + ":" + signature
116
153
  end
117
154
 
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageAliyun
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage-aliyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee