Class: Service::DatabaseService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/database_service.rb

Constant Summary collapse

CHUNK_SIZE =
1.megabytes.freeze

Instance Method Summary collapse

Instance Method Details

#delete(key) ⇒ Object



36
37
38
39
40
# File 'lib/active_storage/service/database_service.rb', line 36

def delete(key)
  instrument :delete, key: key do
    ActivestorageDatabase::File.find_by(key: key)&.destroy
  end
end

#delete_prefixed(prefix) ⇒ Object



42
43
44
45
46
# File 'lib/active_storage/service/database_service.rb', line 42

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    ActivestorageDatabase::File.where("key LIKE ?", "#{prefix}%").destroy_all
  end
end

#download(key, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_storage/service/database_service.rb', line 16

def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      stream(key, &block)
    end
  else
    instrument :download, key: key do
      record = ActivestorageDatabase::File.find_by(key: key)
      record&.data || raise(ActiveStorage::FileNotFoundError)
    end
  end
end

#download_chunk(key, range) ⇒ Object



29
30
31
32
33
34
# File 'lib/active_storage/service/database_service.rb', line 29

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    chunk_select = "SUBSTRING(data FROM #{range.begin + 1} FOR #{range.size}) AS chunk"
    ActivestorageDatabase::File.select(chunk_select).find_by(key: key)&.chunk || raise(ActiveStorage::FileNotFoundError)
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/active_storage/service/database_service.rb', line 48

def exist?(key)
  instrument :exist, key: key do |payload|
    answer = ActivestorageDatabase::File.where(key: key).exists?
    payload[:exist] = answer
    answer
  end
end

#headers_for_direct_upload(_key, content_type:) ⇒ Object



92
93
94
# File 'lib/active_storage/service/database_service.rb', line 92

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

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



7
8
9
10
11
12
13
14
# File 'lib/active_storage/service/database_service.rb', line 7

def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    file = ActivestorageDatabase::File.create!(key: key, data: io.read)
    ensure_integrity_of(key, checksum) if checksum

    file
  end
end

#url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_storage/service/database_service.rb', line 56

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
    )

    url_helpers.service_url(verified_key_with_expiration, filename: filename, **url_options)
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object



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

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
    )

    url_helpers.update_service_url(verified_token_with_expiration, url_options) do |generated_url|
      payload[:url] = generated_url
    end
  end
end