class ActiveStorage::Service::PostgreSQLService
Wraps a PostgreSQL
database as an Active Storage service. See ActiveStorage::Service
for the generic API documentation that applies to all services.
Public Class Methods
new(**options)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 9 def initialize(**options) end
Public Instance Methods
delete(key)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 45 def delete(key) instrument :delete, key: key do ActiveStorage::PostgreSQL::File.find_by(key: key).try(&:destroy) end end
delete_prefixed(prefix)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 59 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do ActiveStorage::PostgreSQL::File.prefixed_with(prefix).destroy_all end end
download(key) { |data| ... }
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 18 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)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 36 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)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 51 def exist?(key) instrument :exist, key: key do |payload| answer = ActiveStorage::PostgreSQL::File.where(key: key).exists? payload[:exist] = answer answer end end
headers_for_direct_upload(key, content_type:, **)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 111 def headers_for_direct_upload(key, content_type:, **) { "Content-Type" => content_type } end
upload(key, io, checksum: nil, **)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 12 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, expires_in:, filename:, disposition:, content_type:)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 65 def 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, host: current_host, disposition: content_disposition, content_type: content_type, filename: filename ) payload[:url] = generated_url generated_url end end
url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 90 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) 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, host: current_host) payload[:url] = generated_url generated_url end end
Protected Instance Methods
current_host()
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 121 def current_host ActiveStorage::Current.host end
url_helpers()
click to toggle source
# File lib/active_storage/service/postgresql_service.rb, line 117 def url_helpers @url_helpers ||= Rails.application.routes.url_helpers end