module Lockbox::ActiveStorageExtensions::Attachment

Public Instance Methods

download() click to toggle source
Calls superclass method
# File lib/lockbox/active_storage_extensions.rb, line 102
def download
  result = super

  options = Utils.encrypted_options(record, name)
  # only trust the metadata when migrating
  # as earlier versions of Lockbox won't have it
  # and it's not a good practice to trust modifiable data
  encrypted = options && (!options[:migrating] || blob.metadata["encrypted"])
  if encrypted
    result = Utils.decrypt_result(record, name, options, result)
  end

  result
end
open(**options) { |file| ... } click to toggle source
# File lib/lockbox/active_storage_extensions.rb, line 128
def open(**options)
  blob.open(**options) do |file|
    options = Utils.encrypted_options(record, name)
    # only trust the metadata when migrating
    # as earlier versions of Lockbox won't have it
    # and it's not a good practice to trust modifiable data
    encrypted = options && (!options[:migrating] || blob.metadata["encrypted"])
    if encrypted
      result = Utils.decrypt_result(record, name, options, file.read)
      file.rewind
      # truncate may not be available on all platforms
      # according to the Ruby docs
      # may need to create a new temp file instead
      file.truncate(0)
      file.write(result)
      file.rewind
    end

    yield file
  end
end
preview(*args) click to toggle source
Calls superclass method
# File lib/lockbox/active_storage_extensions.rb, line 122
def preview(*args)
  raise Lockbox::Error, "Preview not supported for encrypted files" if Utils.encrypted_options(record, name)
  super
end
variant(*args) click to toggle source
Calls superclass method
# File lib/lockbox/active_storage_extensions.rb, line 117
def variant(*args)
  raise Lockbox::Error, "Variant not supported for encrypted files" if Utils.encrypted_options(record, name)
  super
end