aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/site/mirror_site.rb
blob: 67d79a26076d817957fa95f14fca06243b345c5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class ActiveVault::Site::MirrorSite < ActiveVault::Site
  attr_reader :sites

  def initialize(sites:)
    @sites = sites
  end

  def upload(key, io)
    perform_across_sites :upload, key, io
  end

  def download(key)
    sites.detect { |site| site.exist?(key) }.download(key)
  end

  def delete(key)
    perform_across_sites :delete, key
  end

  def exist?(key)
    perform_across_sites(:exist?, key).any?
  end


  def byte_size(key)
    primary_site.byte_size(key)
  end

  def checksum(key)
    primary_site.checksum(key)
  end

  private
    def primary_site
      sites.first
    end

    def perform_across_sites(method, **args)
      # FIXME: Convert to be threaded
      sites.collect do |site|
        site.send method, **args
      end   
    end
end