class ActiveStorage::Service::DBService
Public Class Methods
new(**_config)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 5 def initialize(**_config) @chunk_size = ENV.fetch('ASDB_CHUNK_SIZE') { 1.megabytes } end
Public Instance Methods
delete(key)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 38 def delete(key) instrument :delete, key: key do ::ActiveStorageDB::File.find_by(ref: key)&.destroy end end
delete_prefixed(prefix)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 44 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do ::ActiveStorageDB::File.where('ref LIKE ?', "#{prefix}%").destroy_all end end
download(key, &block)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 17 def download(key, &block) if block_given? instrument :streaming_download, key: key do stream(key, &block) end else instrument :download, key: key do record = ::ActiveStorageDB::File.find_by(ref: key) record&.data || raise(ActiveStorage::FileNotFoundError) end end end
download_chunk(key, range)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 30 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" ::ActiveStorageDB::File.select(chunk_select).find_by(ref: key)&.chunk || raise(ActiveStorage::FileNotFoundError) end end
exist?(key)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 50 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:, **)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 108 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/db_service.rb, line 9 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(key, expires_in:, filename:, disposition:, content_type:)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 58 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 ) current_uri = URI.parse(current_host) generated_url = url_helpers.service_url( verified_key_with_expiration, protocol: current_uri.scheme, host: current_uri.host, port: current_uri.port, 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/db_service.rb, line 86 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_service_url( verified_token_with_expiration, host: current_host ) payload[:url] = generated_url generated_url end end
Private Instance Methods
current_host()
click to toggle source
# File lib/active_storage/service/db_service.rb, line 114 def current_host ActiveStorage::Current.host end
ensure_integrity_of(key, checksum)
click to toggle source
# File lib/active_storage/service/db_service.rb, line 118 def ensure_integrity_of(key, checksum) file = ::ActiveStorageDB::File.find_by(ref: key) return if Digest::MD5.base64digest(file.data) == checksum delete(key) raise ActiveStorage::IntegrityError end
stream(key) { |download_chunk(key, range)| ... }
click to toggle source
# File lib/active_storage/service/db_service.rb, line 126 def stream(key) size = ::ActiveStorageDB::File.select('OCTET_LENGTH(data) AS size').find_by(ref: key)&.size || raise(ActiveStorage::FileNotFoundError) (size / @chunk_size.to_f).ceil.times.each do |i| range = (i * @chunk_size..(i + 1) * @chunk_size - 1) yield download_chunk(key, range) end end
url_helpers()
click to toggle source
# File lib/active_storage/service/db_service.rb, line 136 def url_helpers @url_helpers ||= ::ActiveStorageDB::Engine.routes.url_helpers end