activestorage-aliyun 0.2.0 → 0.3.0
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 +5 -0
- data/README.md +19 -3
- data/lib/active_storage/service/aliyun_service.rb +45 -8
- data/lib/active_storage_aliyun/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 796ec942f06897cf76873d1b6dadeb23a565345f
|
4
|
+
data.tar.gz: fd06616edba762ac6ccf4e352cbec7a17fc220dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
42
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
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
|
149
|
+
addition_headers = "x-oss-date:#{date}"
|
113
150
|
sign = ["PUT", checksum, content_type, date, addition_headers, filename].join("\n")
|
114
|
-
signature =
|
151
|
+
signature = bucket.sign(sign)
|
115
152
|
"OSS " + config.fetch(:access_key_id) + ":" + signature
|
116
153
|
end
|
117
154
|
|