Class: Service::DBService
- Inherits:
-
Service
- Object
- Service
- Service::DBService
- Includes:
- ActiveStorage::DBServiceRails60, ActiveStorage::DBServiceRails61, ActiveStorage::DBServiceRails70
- Defined in:
- lib/active_storage/service/db_service.rb
Overview
Wraps a DB table 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, &block) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(_key, content_type:) ⇒ Object
-
#initialize(public: false) ⇒ DBService
constructor
:nocov:.
- #upload(key, io, checksum: nil) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
Methods included from ActiveStorage::DBServiceRails60
Methods included from ActiveStorage::DBServiceRails70
Constructor Details
#initialize(public: false) ⇒ DBService
:nocov:
21 22 23 24 |
# File 'lib/active_storage/service/db_service.rb', line 21 def initialize(public: false, **) @chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes } @public = public end |
Instance Method Details
#delete(key) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/active_storage/service/db_service.rb', line 58 def delete(key) instrument :delete, key: key do ::ActiveStorageDB::File.find_by(ref: key)&.destroy # Ignore files already deleted end end |
#delete_prefixed(prefix) ⇒ Object
65 66 67 68 69 |
# File 'lib/active_storage/service/db_service.rb', line 65 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do ::ActiveStorageDB::File.where('ref LIKE ?', "#{ApplicationRecord.sanitize_sql_like(prefix)}%").destroy_all end end |
#download(key, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_storage/service/db_service.rb', line 34 def download(key, &block) if block_given? instrument :streaming_download, key: key do stream(key, &block) end else instrument :download, key: key do retrieve_file(key) end end end |
#download_chunk(key, range) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_storage/service/db_service.rb', line 46 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do from = range.begin + 1 size = range.size args = adapter_sqlserver? || adapter_sqlite? ? "data, #{from}, #{size}" : "data FROM #{from} FOR #{size}" record = object_for(key, fields: "SUBSTRING(#{args}) AS chunk") raise(ActiveStorage::FileNotFoundError) unless record record.chunk end end |
#exist?(key) ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'lib/active_storage/service/db_service.rb', line 71 def exist?(key) instrument :exist, key: key do |payload| answer = ::ActiveStorageDB::File.where(ref: key).exists? payload[:exist] = answer answer end end |
#headers_for_direct_upload(_key, content_type:) ⇒ Object
99 100 101 |
# File 'lib/active_storage/service/db_service.rb', line 99 def headers_for_direct_upload(_key, content_type:, **) { 'Content-Type' => content_type } end |
#upload(key, io, checksum: nil) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/active_storage/service/db_service.rb', line 26 def upload(key, io, checksum: nil, **) instrument :upload, key: key, checksum: checksum do file = ::ActiveStorageDB::File.create!(ref: key, data: io.read) ensure_integrity_of(key, checksum) if checksum file end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_storage/service/db_service.rb', line 79 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, service_name: respond_to?(:name) ? name : 'db' }, expires_in: expires_in, purpose: :blob_token ) url_helpers.update_service_url(verified_token_with_expiration, ).tap do |generated_url| payload[:url] = generated_url end end end |