Class: ActiveStorageEncryption::EncryptedMirrorService

Inherits:
ActiveStorage::Service::MirrorService
  • Object
show all
Defined in:
lib/active_storage_encryption/encrypted_mirror_service.rb

Defined Under Namespace

Classes: MirrorJobWithEncryption

Instance Method Summary collapse

Instance Method Details

#encrypted?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/active_storage_encryption/encrypted_mirror_service.rb', line 31

def encrypted?
  true
end

#mirror_with_encryption(key, checksum:, encryption_key:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_storage_encryption/encrypted_mirror_service.rb', line 45

def mirror_with_encryption(key, checksum:, encryption_key:)
  instrument :mirror, key: key, checksum: checksum do
    mirrors_in_need_of_mirroring = mirrors.select { |service| !service.exist?(key) }
    return if mirrors_in_need_of_mirroring.empty?
    primary.open(key, checksum: checksum, verify: checksum.present?, encryption_key: encryption_key) do |io|
      mirrors_in_need_of_mirroring.each do |target|
        io.rewind
        options = target.try(:encrypted?) ? {encryption_key: encryption_key} : {}
        target.upload(key, io, checksum: checksum, **options)
      end
    end
  end
end

#private_url_policy=(_) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
# File 'lib/active_storage_encryption/encrypted_mirror_service.rb', line 27

def private_url_policy=(_)
  raise ArgumentError, "EncryptedMirrorService uses the private_url_policy of the primary"
end

#service_nameObject



59
60
61
62
63
# File 'lib/active_storage_encryption/encrypted_mirror_service.rb', line 59

def service_name
  # ActiveStorage::Service::DiskService => Disk
  # Overridden because in Rails 8 this is "self.class.name.split("::").third.remove("Service")"
  self.class.name.split("::").last.remove("Service")
end

#upload(key, io, encryption_key:, checksum: nil, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/active_storage_encryption/encrypted_mirror_service.rb', line 35

def upload(key, io, encryption_key:, checksum: nil, **options)
  io.rewind
  if primary.try(:encrypted?)
    primary.upload(key, io, checksum: checksum, encryption_key: encryption_key, **options)
  else
    primary.upload(key, io, checksum: checksum, **options)
  end
  mirror_later_with_encryption(key, checksum: checksum, encryption_key: encryption_key, **options)
end