vng_storage_active_storage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 332e66ee8bd3c5da936ea1f0d42427d55cb2d7aaf1ff3430401dbb1bf037a1b6
4
+ data.tar.gz: e6052ba32ff53aef1fba86ace5cc7b0391eb4700f7c894ad75a3980b8b4e5c16
5
+ SHA512:
6
+ metadata.gz: 78a74f8b09c2e4510b3f0594cf7d5ad9bf4e16aafeda1905618baba0a80a55838e3c909e03d95b057ab0cc09466deb3f6f55c73ac900578eaeed0cced45c7416
7
+ data.tar.gz: 4b9405f7098247ce6502b991077c81f86d2f0419e1e1c73b0a2b343e0e5f65bc8121dd5be4bb1fe2909b5b776fc8f6d806c3a06db9a1e58f407b1d206bda4586
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ VNG Storage ActiveStorage is a custom service of ActiveStorage to support [VNG Cloud](https://vngcloud.vn). This service is reuse S3 SDK from S3 Service of ActiveStorage.
2
+
3
+ # Installation
4
+
5
+ ```sh
6
+ gem 'vng_storage_active_storage'
7
+ ```
8
+
9
+ # Configuration
10
+
11
+ ```yaml
12
+ vstorage:
13
+ service: Vstorage
14
+ endpoint: <%= ENV['VCLOUD_ENDPOINT'] %>
15
+ region: <%= ENV['VCLOUD_REGION'] %>
16
+ bucket: <%= ENV['VCLOUD_BUCKET'] %>
17
+ access_key_id: <%= ENV['VCLOUD_ACCESS_KEY'] %>
18
+ secret_access_key: <%= ENV['VCLOUD_SECRET_KEY'] %>
19
+ public: true
20
+ force_path_style: true
21
+ upload:
22
+ project_id: <%= ENV['VCLOUD_PROJECT_ID'] %>
23
+ client_id: <%= ENV['VCLOUD_SERVICE_ACCOUNT_CLIENT_ID'] %>
24
+ client_secret: <%= ENV['VCLOUD_SERVICE_ACCOUNT_CLIENT_SECRET'] %>
25
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'net/http'
5
+ require 'active_storage/service/s3_service'
6
+
7
+ module ActiveStorage
8
+ class Service
9
+ class VstorageService < S3Service
10
+ attr_reader :region,
11
+ :client_id,
12
+ :project_id,
13
+ :client_secret,
14
+ :authorization
15
+
16
+ def initialize(bucket:, upload: {}, public: false, **options)
17
+ @region = options[:region].downcase
18
+ @project_id = upload.delete(:project_id)
19
+ @client_id = upload.delete(:client_id)
20
+ @client_secret = upload.delete(:client_secret)
21
+ @authorization = Base64.strict_encode64("#{@client_id}:#{@client_secret}")
22
+
23
+ super(bucket: bucket, upload: upload, public: public, **options)
24
+ end
25
+
26
+ def url_for_direct_upload(key, expires_in:, **)
27
+ instrument(:url, key: key) do |payload|
28
+ generated_url = temp_url(bucket, key, expires_in)
29
+ payload[:url] = generated_url
30
+ generated_url
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def public_url(key, **)
37
+ "https://#{region}.vstorage.vngcloud.vn/v1/AUTH_#{project_id}/#{bucket.name}/#{key}"
38
+ end
39
+
40
+ def temp_url(bucket, key, expires_in)
41
+ response = RestClient.post(
42
+ "https://#{region}-api.vstorage.vngcloud.vn/api/v1/projects/#{project_id}/containers/#{bucket.name}/objects/#{key}/upload_tempurls",
43
+ {
44
+ timeExpire: expires_in.to_i
45
+ }.to_json,
46
+ {
47
+ 'Content-Type': 'application/json',
48
+ Authorization: "Bearer #{access_token}"
49
+ }
50
+ )
51
+
52
+ JSON.parse(response.body).with_indifferent_access[:data][key]
53
+ rescue StandardError => e
54
+ raise VngStorageActiveStorage::Error, "Request temp url failed with error: #{e.message}"
55
+ end
56
+
57
+ def access_token
58
+ Rails.cache.fetch("vstorage_access_token_#{project_id}", expires_in: 15.minutes) do
59
+ response = RestClient.post(
60
+ 'https://iamapis.vngcloud.vn/accounts-api/v2/auth/token',
61
+ {
62
+ scope: 'email',
63
+ grant_type: 'client_credentials'
64
+ },
65
+ {
66
+ 'Content-Type': 'application/json',
67
+ Authorization: "Basic #{authorization}"
68
+ }
69
+ )
70
+
71
+ JSON.parse(response.body).with_indifferent_access[:access_token]
72
+ end
73
+ rescue StandardError => e
74
+ raise VngStorageActiveStorage::Error, "Request Access Token failed with error: #{e.message}"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VngStorageActiveStorage
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vng_storage_active_storage/version'
4
+
5
+ module VngStorageActiveStorage
6
+ class Error < StandardError; end
7
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/vng_storage_active_storage/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'vng_storage_active_storage'
7
+ spec.version = VngStorageActiveStorage::VERSION
8
+ spec.authors = ['Alpha']
9
+ spec.email = ['alphanolucifer@gmail.com']
10
+
11
+ spec.summary = 'ActiveStorage Service for VNG Storage'
12
+ spec.description = 'ActiveStorage Service for VNG Storage'
13
+ spec.homepage = 'https://github.com/zgid123/vng_storage_active_storage'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (File.expand_path(f) == __FILE__) ||
22
+ f.start_with?(
23
+ *%w[
24
+ bin/
25
+ test/
26
+ spec/
27
+ features/
28
+ .git
29
+ .circleci
30
+ appveyor
31
+ examples/
32
+ Gemfile
33
+ .rubocop.yml
34
+ .vscode/settings.json
35
+ LICENSE.txt
36
+ lefthook.yml
37
+ ]
38
+ )
39
+ end
40
+ end
41
+
42
+ spec.require_paths = ['lib']
43
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vng_storage_active_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alpha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ActiveStorage Service for VNG Storage
14
+ email:
15
+ - alphanolucifer@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".ruby-version"
21
+ - README.md
22
+ - Rakefile
23
+ - lib/active_storage/service/vstorage_service.rb
24
+ - lib/vng_storage_active_storage.rb
25
+ - lib/vng_storage_active_storage/version.rb
26
+ - vng_storage_active_storage.gemspec
27
+ homepage: https://github.com/zgid123/vng_storage_active_storage
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://github.com/zgid123/vng_storage_active_storage
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.6.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.4.13
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: ActiveStorage Service for VNG Storage
51
+ test_files: []