Files
docuseal/lib/load_active_storage_configs.rb
T

43 lines
1.2 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
module LoadActiveStorageConfigs
STORAGE_YML_PATH = Rails.root.join('config/storage.yml')
2023-06-30 20:31:17 +03:00
IS_ENV_CONFIGURED =
ENV['S3_ATTACHMENTS_BUCKET'].present? || ENV['GCS_BUCKET'].present? || ENV['AZURE_CONTAINER'].present?
2023-05-21 17:59:36 +03:00
module_function
def call
reload unless loaded?
end
def loaded?
@loaded
end
def reload
2023-06-30 20:31:17 +03:00
return if Docuseal.multitenant?
return if IS_ENV_CONFIGURED
2023-08-20 12:42:47 +03:00
return if Rails.env.test?
2023-06-30 20:31:17 +03:00
2023-05-21 17:59:36 +03:00
encrypted_config = EncryptedConfig.find_by(key: EncryptedConfig::FILES_STORAGE_KEY)
return unless encrypted_config
service, configs = encrypted_config.value.values_at('service', 'configs')
service_configurations = ActiveSupport::ConfigurationFile.parse(STORAGE_YML_PATH)
service_configurations[service].merge!(configs) if configs.present?
service_configurations[service][:force_path_style] = true if configs&.dig('endpoint').present?
if service == 'google'
service_configurations[service][:credentials] = JSON.parse(configs.fetch('credentials', '{}'))
end
ActiveStorage::Blob.services = ActiveStorage::Service::Registry.new(service_configurations)
ActiveStorage::Blob.service = ActiveStorage::Blob.services.fetch(service.to_sym)
ensure
@loaded = true
end
end