Class: Service::DBService

Inherits:
Service
  • Object
show all
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

Methods included from ActiveStorage::DBServiceRails60

#url

Methods included from ActiveStorage::DBServiceRails70

#compose

Constructor Details

#initialize(public: false) ⇒ DBService

Returns a new instance of DBService.



19
20
21
22
# File 'lib/active_storage/service/db_service.rb', line 19

def initialize(public: false, **)
  @chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes }
  @public = public
end

Instance Method Details

#delete(key) ⇒ Object



53
54
55
56
57
58
# File 'lib/active_storage/service/db_service.rb', line 53

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



60
61
62
63
64
# File 'lib/active_storage/service/db_service.rb', line 60

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



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_storage/service/db_service.rb', line 32

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



44
45
46
47
48
49
50
51
# File 'lib/active_storage/service/db_service.rb', line 44

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    record = object_for(key, fields: "SUBSTRING(data FROM #{range.begin + 1} FOR #{range.size}) AS chunk")
    raise(ActiveStorage::FileNotFoundError) unless record

    record.chunk
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/active_storage/service/db_service.rb', line 66

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



94
95
96
# File 'lib/active_storage/service/db_service.rb', line 94

def headers_for_direct_upload(_key, content_type:, **)
  { 'Content-Type' => content_type }
end

#upload(key, io, checksum: nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_storage/service/db_service.rb', line 24

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_storage/service/db_service.rb', line 74

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, url_options).tap do |generated_url|
      payload[:url] = generated_url
    end
  end
end