activestorage-aliyun 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8561c1bab22046a3249849fa1198ecbdc6929b1f
4
- data.tar.gz: 78758224fd12173075a2b3d79a5896d0361d43d1
3
+ metadata.gz: bfac27721ec3ce8613444b343030117a4ddf6d28
4
+ data.tar.gz: 7ed58307e3dec06209a29436127d5f4f188dae0f
5
5
  SHA512:
6
- metadata.gz: 0a137d16f6ddf427e374e92d951bd074dc1647e74c226a06d82ce8a4870e68b98653ea2d07b1ebd6a54586aca98d8d1ebdb684341368e8f9c3448adf5901e2f2
7
- data.tar.gz: 8db2b3039051f23371d88c7f556f7ae39d12b75a39a7bd419b37630616d370a296daefb776fdbc59d73521158e2da1936e343079fd43a7286d25f47110d4006d
6
+ metadata.gz: 7dfb3e7aa65baa48d1040fcb8b11f867b1fab78a0eedb21b7ce1ca7ab37d05b0e56521c61fc5ca195f2f72adeaf6ed66b346bcca0349e9b9bf7ec1a2c7c27f46
7
+ data.tar.gz: dcf684e6ff9ffce4ef3e75ec5f7db26513cad9cf8ce45de3a68a501fd048a95365ab396185d6e6d28868d7e53f66e0f904d3676e10e69e969d4d68baf5b1e617
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.0
2
+
3
+ - Add url_for_direct_upload support.
4
+ - Fix delete_prefixed error when path not exists.
5
+
1
6
  ## 0.1.1
2
7
 
3
8
  - Fix streaming upload.
@@ -39,7 +39,9 @@ module ActiveStorage
39
39
  def delete_prefixed(prefix)
40
40
  instrument :delete_prefixed, prefix: prefix do
41
41
  files = bucket.list_objects(prefix: path_for(prefix))
42
+ return if files.blank?
42
43
  keys = files.map(&:key)
44
+ return if keys.blank?
43
45
  bucket.batch_delete_objects(keys, quiet: true)
44
46
  end
45
47
  end
@@ -54,7 +56,7 @@ module ActiveStorage
54
56
  instrument :url, key: key do |payload|
55
57
  generated_url = bucket.object_url(path_for(key), false, expires_in)
56
58
  generated_url.gsub('http://', 'https://')
57
- if filename.present? && filename.include?("x-oss-process")
59
+ if filename.to_s.include?("x-oss-process")
58
60
  generated_url = [generated_url, filename].join("?")
59
61
  end
60
62
 
@@ -64,15 +66,31 @@ module ActiveStorage
64
66
  end
65
67
  end
66
68
 
69
+ # You must setup CORS on OSS control panel to allow JavaScript request from your site domain.
70
+ # https://www.alibabacloud.com/help/zh/doc-detail/31988.htm
71
+ # https://help.aliyun.com/document_detail/31925.html
72
+ # Source: *.your.host.com
73
+ # Allowed Methods: POST, PUT, HEAD
74
+ # Allowed Headers: *
67
75
  def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
68
76
  instrument :url, key: key do |payload|
69
- # FIXME: to implement direct upload
70
- raise 'Not implement'
77
+ generated_url = bucket.object_url(path_for(key), false)
78
+ payload[:url] = generated_url
79
+ generated_url
71
80
  end
72
81
  end
73
82
 
83
+ # Headers for Direct Upload
84
+ # https://help.aliyun.com/document_detail/31951.html
85
+ # headers["Date"] is required use x-oss-date instead
74
86
  def headers_for_direct_upload(key, content_type:, checksum:, **)
75
- { "Content-Type" => content_type, "Content-MD5" => checksum }
87
+ date = Time.now.httpdate
88
+ {
89
+ "Content-Type" => content_type,
90
+ "Content-MD5" => checksum,
91
+ "Authorization" => authorization(key, content_type, checksum, date),
92
+ "x-oss-date" => date,
93
+ }
76
94
  end
77
95
 
78
96
  private
@@ -89,6 +107,14 @@ module ActiveStorage
89
107
  @bucket
90
108
  end
91
109
 
110
+ def authorization(key, content_type, checksum, date)
111
+ filename = File.expand_path("/#{bucket.name}/#{path_for(key)}")
112
+ addition_headers = "x-oss-date:"+date
113
+ 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))
115
+ "OSS " + config.fetch(:access_key_id) + ":" + signature
116
+ end
117
+
92
118
  def endpoint
93
119
  config.fetch(:endpoint, "https://oss-cn-hangzhou.aliyuncs.com")
94
120
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageAliyun
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee