vng_storage_active_storage 0.1.0 → 0.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66b0caefb70e7135f08ea9cdbcbb4476445466387e25c33b25b98b63857c3523
|
4
|
+
data.tar.gz: 68cab371e85e993a80a0f3dd1cd322959c608c8756ec042655c4cad669a36184
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e73f41bf7082f7e07f8978cff17fa1360139746a1501e8bcbc27cd7132adb2c88d86542b9bff74935a58ff8fb38c485c6dc90a136341234980e8026d5eb4d780
|
7
|
+
data.tar.gz: c5fb3d9933f041b4e147bebaa33506f8e33643c885b3a94fbb16164dbd6bc2036a0f9321e84c9114340e16027f0dd167148ee8cbb2be98f44b33d03ae6b6bca1
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'base64'
|
4
|
-
require 'net/http'
|
5
4
|
require 'active_storage/service/s3_service'
|
6
5
|
|
7
6
|
module ActiveStorage
|
@@ -38,37 +37,37 @@ module ActiveStorage
|
|
38
37
|
end
|
39
38
|
|
40
39
|
def temp_url(bucket, key, expires_in)
|
41
|
-
response =
|
42
|
-
"https://#{region}-api.vstorage.vngcloud.vn/api/v1/projects/#{project_id}/containers/#{bucket.name}/objects/#{key}/upload_tempurls",
|
43
|
-
{
|
40
|
+
response = VngStorageActiveStorage::RequestClient.post(
|
41
|
+
url: "https://#{region}-api.vstorage.vngcloud.vn/api/v1/projects/#{project_id}/containers/#{bucket.name}/objects/#{key}/upload_tempurls",
|
42
|
+
body: {
|
44
43
|
timeExpire: expires_in.to_i
|
45
|
-
}
|
46
|
-
{
|
44
|
+
},
|
45
|
+
headers: {
|
47
46
|
'Content-Type': 'application/json',
|
48
47
|
Authorization: "Bearer #{access_token}"
|
49
48
|
}
|
50
49
|
)
|
51
50
|
|
52
|
-
|
51
|
+
response[:data][key]
|
53
52
|
rescue StandardError => e
|
54
53
|
raise VngStorageActiveStorage::Error, "Request temp url failed with error: #{e.message}"
|
55
54
|
end
|
56
55
|
|
57
56
|
def access_token
|
58
57
|
Rails.cache.fetch("vstorage_access_token_#{project_id}", expires_in: 15.minutes) do
|
59
|
-
response =
|
60
|
-
'https://iamapis.vngcloud.vn/accounts-api/v2/auth/token',
|
61
|
-
{
|
58
|
+
response = VngStorageActiveStorage::RequestClient.post(
|
59
|
+
url: 'https://iamapis.vngcloud.vn/accounts-api/v2/auth/token',
|
60
|
+
body: {
|
62
61
|
scope: 'email',
|
63
62
|
grant_type: 'client_credentials'
|
64
63
|
},
|
65
|
-
{
|
64
|
+
headers: {
|
66
65
|
'Content-Type': 'application/json',
|
67
66
|
Authorization: "Basic #{authorization}"
|
68
67
|
}
|
69
68
|
)
|
70
69
|
|
71
|
-
|
70
|
+
response[:access_token]
|
72
71
|
end
|
73
72
|
rescue StandardError => e
|
74
73
|
raise VngStorageActiveStorage::Error, "Request Access Token failed with error: #{e.message}"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
module VngStorageActiveStorage
|
7
|
+
class RequestClient
|
8
|
+
class << self
|
9
|
+
def post(url:, body:, headers: nil)
|
10
|
+
response = make_request(url, Net::HTTP::Post, body, headers)
|
11
|
+
|
12
|
+
handle_response(response)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def make_request(url, method_class, body, headers)
|
18
|
+
uri = URI.parse(url)
|
19
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
20
|
+
http.use_ssl = true
|
21
|
+
|
22
|
+
request = method_class.new(
|
23
|
+
uri.path,
|
24
|
+
headers || { 'Content-Type' => 'application/json' }
|
25
|
+
)
|
26
|
+
|
27
|
+
request.body = body.to_json unless method_class.is_a?(Net::HTTP::Get)
|
28
|
+
|
29
|
+
http.request(request)
|
30
|
+
end
|
31
|
+
|
32
|
+
def handle_response(response)
|
33
|
+
raise JSON.parse(response.body).with_indifferent_access[:errors].first[:message] unless success?(response) # handle error in vstorage_service
|
34
|
+
|
35
|
+
JSON.parse(response.body).with_indifferent_access
|
36
|
+
end
|
37
|
+
|
38
|
+
def success?(response)
|
39
|
+
code = response.code.to_i
|
40
|
+
|
41
|
+
code >= 200 && code < 300
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vng_storage_active_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alpha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ActiveStorage Service for VNG Storage
|
14
14
|
email:
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- Rakefile
|
23
23
|
- lib/active_storage/service/vstorage_service.rb
|
24
24
|
- lib/vng_storage_active_storage.rb
|
25
|
+
- lib/vng_storage_active_storage/request_client.rb
|
25
26
|
- lib/vng_storage_active_storage/version.rb
|
26
27
|
- vng_storage_active_storage.gemspec
|
27
28
|
homepage: https://github.com/zgid123/vng_storage_active_storage
|