aboutsummaryrefslogblamecommitdiffstats
path: root/lib/active_storage/service/mirror_service.rb
blob: 0d37ad96a3b1f68501d3db319ab853861de5e96f (plain) (tree)
1
2
3
4
5
6
7

                                                   


                                                                    

                                                                                










                                 



                                        











                                              
require "active_support/core_ext/module/delegation"

class ActiveStorage::Service::MirrorService < ActiveStorage::Service
  attr_reader :services

  delegate :download, :exist?, :url, :byte_size, :checksum, to: :primary_service

  def initialize(services:)
    @services = services
  end

  def upload(key, io)
    services.collect do |service|
      service.upload key, io
      io.rewind
    end
  end

  def delete(key)
    perform_across_services :delete, key
  end

  private
    def primary_service
      services.first
    end

    def perform_across_services(method, *args)
      # FIXME: Convert to be threaded
      services.collect do |service|
        service.public_send method, *args
      end
    end
end