Wraps a set of mirror services and provides a single ActiveStorage::Service object that will all have the files uploaded to them. A primary service is designated to answer calls to:

  • download

  • exists?

  • url

  • url_for_direct_upload

  • headers_for_direct_upload

Methods

Attributes

[R] mirrors
[R] primary

Class Public methods

new(primary:, mirrors:)

📝 Source code
# File activestorage/lib/active_storage/service/mirror_service.rb, line 29
    def initialize(primary:, mirrors:)
      @primary, @mirrors = primary, mirrors
    end
🔎 See on GitHub

Instance Public methods

delete(key)

Delete the file at the key on all services.

📝 Source code
# File activestorage/lib/active_storage/service/mirror_service.rb, line 43
    def delete(key)
      perform_across_services :delete, key
    end
🔎 See on GitHub

delete_prefixed(prefix)

Delete files at keys starting with the prefix on all services.

📝 Source code
# File activestorage/lib/active_storage/service/mirror_service.rb, line 48
    def delete_prefixed(prefix)
      perform_across_services :delete_prefixed, prefix
    end
🔎 See on GitHub

mirror(key, checksum:)

Copy the file at the key from the primary service to each of the mirrors where it doesn't already exist.

📝 Source code
# File activestorage/lib/active_storage/service/mirror_service.rb, line 54
    def mirror(key, checksum:)
      instrument :mirror, key: key, checksum: checksum do
        if (mirrors_in_need_of_mirroring = mirrors.select { |service| !service.exist?(key) }).any?
          primary.open(key, checksum: checksum) do |io|
            mirrors_in_need_of_mirroring.each do |service|
              io.rewind
              service.upload key, io, checksum: checksum
            end
          end
        end
      end
    end
🔎 See on GitHub

upload(key, io, checksum: nil, **options)

Upload the io to the key specified to all services. If a checksum is provided, all services will ensure a match when the upload has completed or raise an ActiveStorage::IntegrityError.

📝 Source code
# File activestorage/lib/active_storage/service/mirror_service.rb, line 35
    def upload(key, io, checksum: nil, **options)
      each_service.collect do |service|
        io.rewind
        service.upload key, io, checksum: checksum, **options
      end
    end
🔎 See on GitHub