From 6d7141db97f5ddd932fd499564db2ef47b18f745 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sat, 21 Mar 2026 11:52:14 +0100 Subject: [PATCH] feat: Resolves the correct service in FilesController Add db_service_for(service_name) that resolves the correct service from ActiveStorage::Blob.services (when available). --- .../active_storage_db/files_controller.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/controllers/active_storage_db/files_controller.rb b/app/controllers/active_storage_db/files_controller.rb index 6601737..b72bbb1 100644 --- a/app/controllers/active_storage_db/files_controller.rb +++ b/app/controllers/active_storage_db/files_controller.rb @@ -6,7 +6,8 @@ class FilesController < ActiveStorage::BaseController def show if (key = decode_verified_key) - serve_file(key[:key], content_type: key[:content_type], disposition: key[:disposition]) + attrs = key.slice(:content_type, :disposition, :service_name) + serve_file(key[:key], **attrs) else head(:not_found) end @@ -32,8 +33,12 @@ def acceptable_content?(token) token[:content_length] == request.content_length end - def db_service - ActiveStorage::Blob.service + def db_service_for(service_name) + if service_name && ActiveStorage::Blob.respond_to?(:services) + ActiveStorage::Blob.services.fetch(service_name) { ActiveStorage::Blob.service } + else + ActiveStorage::Blob.service + end end def decode_verified_key @@ -46,18 +51,19 @@ def decode_verified_token token&.deep_symbolize_keys end - def serve_file(key, content_type:, disposition:) + def serve_file(key, content_type:, disposition:, service_name: nil) options = { type: content_type || DEFAULT_SEND_FILE_TYPE, disposition: disposition || DEFAULT_SEND_FILE_DISPOSITION } - send_data(db_service.download(key), options) + send_data(db_service_for(service_name).download(key), options) end def upload_file(token) # rubocop:disable Naming/PredicateMethod return false unless acceptable_content?(token) - db_service.upload(token[:key], request.body, checksum: token[:checksum]) + service = db_service_for(token[:service_name]) + service.upload(token[:key], request.body, checksum: token[:checksum]) true end end