class ActiveStorage::Service::CloudinaryService

def self.fetch_service_instance_and_config(source, options)

def self.fetch_service_instance_and_config(source, options)
  return [nil, options] unless defined?(ActiveStorage::BlobKey) && source.is_a?(ActiveStorage::BlobKey) &&
    source.respond_to?(:attributes) && source.attributes.key?(:service_name)
  service_name = source.attributes[:service_name]
  begin
    service_instance = ActiveStorage::Blob.services.fetch(service_name.to_sym)
    unless service_instance.instance_of?(ActiveStorage::Service::CloudinaryService)
      Rails.logger.error "Expected service instance #{service_instance.class.name} to be of type ActiveStorage::Service::CloudinaryService."
      return [nil, options]
    end
    service_config = Rails.application.config.active_storage.service_configurations.fetch(service_name)
    options        = service_config.merge(options)
  rescue NameError => e
    Rails.logger.error "Failed to retrieve the service instance for #{service_name}: #{e.message}"
    return [nil, options]
  rescue => e
    Rails.logger.error "An unexpected error occurred: #{e.message}"
    return [nil, options]
  end
  [service_instance, options]
end