Class: Service::PostgreSQLService
- Inherits:
-
Service
- Object
- Service
- Service::PostgreSQLService
- Defined in:
- lib/active_storage/service/postgresql_service.rb
Overview
Wraps a PostgreSQL database as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #generate_url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object
- #headers_for_direct_upload(key, content_type:) ⇒ Object
-
#initialize(public: false, **options) ⇒ PostgreSQLService
constructor
A new instance of PostgreSQLService.
- #private_url(key, expires_in:, filename:, content_type:, disposition:) ⇒ Object
- #public_url(key, filename:, content_type: nil, disposition: :attachment) ⇒ Object
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, **options) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
Constructor Details
#initialize(public: false, **options) ⇒ PostgreSQLService
Returns a new instance of PostgreSQLService.
7 8 9 |
# File 'lib/active_storage/service/postgresql_service.rb', line 7 def initialize(public: false, **) @public = public end |
Instance Method Details
#delete(key) ⇒ Object
44 45 46 47 48 |
# File 'lib/active_storage/service/postgresql_service.rb', line 44 def delete(key) instrument :delete, key: key do ActiveStorage::PostgreSQL::File.find_by(key: key).try(&:destroy) end end |
#delete_prefixed(prefix) ⇒ Object
58 59 60 61 62 |
# File 'lib/active_storage/service/postgresql_service.rb', line 58 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do ActiveStorage::PostgreSQL::File.prefixed_with(prefix).destroy_all end end |
#download(key) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/active_storage/service/postgresql_service.rb', line 17 def download(key) if block_given? instrument :streaming_download, key: key do ActiveStorage::PostgreSQL::File.open(key) do |file| while data = file.read(5.megabytes) yield data end end end else instrument :download, key: key do ActiveStorage::PostgreSQL::File.open(key) do |file| file.read end end end end |
#download_chunk(key, range) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/active_storage/service/postgresql_service.rb', line 35 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do ActiveStorage::PostgreSQL::File.open(key) do |file| file.seek(range.first) file.read(range.size) end end end |
#exist?(key) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/active_storage/service/postgresql_service.rb', line 50 def exist?(key) instrument :exist, key: key do |payload| answer = ActiveStorage::PostgreSQL::File.where(key: key).exists? payload[:exist] = answer answer end end |
#generate_url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_storage/service/postgresql_service.rb', line 82 def generate_url(key, expires_in:, filename:, disposition:, content_type:) instrument :url, key: key do |payload| content_disposition = content_disposition_with(type: disposition, filename: filename) verified_key_with_expiration = ActiveStorage.verifier.generate( { key: key, disposition: content_disposition, content_type: content_type }, expires_in: expires_in, purpose: :blob_key ) generated_url = url_helpers.rails_postgresql_service_url(verified_key_with_expiration, **, disposition: content_disposition, content_type: content_type, filename: filename ) payload[:url] = generated_url generated_url end end |
#headers_for_direct_upload(key, content_type:) ⇒ Object
128 129 130 |
# File 'lib/active_storage/service/postgresql_service.rb', line 128 def headers_for_direct_upload(key, content_type:, **) { "Content-Type" => content_type } end |
#private_url(key, expires_in:, filename:, content_type:, disposition:) ⇒ Object
64 65 66 |
# File 'lib/active_storage/service/postgresql_service.rb', line 64 def private_url(key, expires_in:, filename:, content_type:, disposition:, **) generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition) end |
#public_url(key, filename:, content_type: nil, disposition: :attachment) ⇒ Object
68 69 70 |
# File 'lib/active_storage/service/postgresql_service.rb', line 68 def public_url(key, filename:, content_type: nil, disposition: :attachment, **) generate_url(key, expires_in: nil, filename: filename, content_type: content_type, disposition: disposition) end |
#upload(key, io, checksum: nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/active_storage/service/postgresql_service.rb', line 11 def upload(key, io, checksum: nil, **) instrument :upload, key: key, checksum: checksum do ActiveStorage::PostgreSQL::File.create!(key: key, io: io, checksum: checksum) end end |
#url(key, **options) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/active_storage/service/postgresql_service.rb', line 72 def url(key, **) super rescue NotImplementedError, ArgumentError if @public public_url(key, **) else private_url(key, **) end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/active_storage/service/postgresql_service.rb', line 107 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) instrument :url, key: key do |payload| verified_token_with_expiration = ActiveStorage.verifier.generate( { key: key, content_type: content_type, content_length: content_length, checksum: checksum }, expires_in: expires_in, purpose: :blob_token ) generated_url = url_helpers.update_rails_postgresql_service_url(verified_token_with_expiration, **) payload[:url] = generated_url generated_url end end |