Module: Lockbox::ActiveStorageExtensions::Attachment

Defined in:
lib/lockbox/active_storage_extensions.rb

Instance Method Summary collapse

Instance Method Details

#downloadObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lockbox/active_storage_extensions.rb', line 82

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.["encrypted"])
  if encrypted
    result = Utils.decrypt_result(record, name, options, result)
  end

  result
end

#open(**options) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lockbox/active_storage_extensions.rb', line 114

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.["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) ⇒ Object

Raises:



102
103
104
105
# File 'lib/lockbox/active_storage_extensions.rb', line 102

def preview(*args)
  raise Lockbox::Error, "Preview not supported for encrypted files" if Utils.encrypted_options(record, name)
  super
end

#transform_variants_laterObject



108
109
110
111
# File 'lib/lockbox/active_storage_extensions.rb', line 108

def transform_variants_later
  blob.instance_variable_set(:@lockbox_encrypted, true) if Utils.encrypted_options(record, name)
  super
end

#variant(*args) ⇒ Object

Raises:



97
98
99
100
# File 'lib/lockbox/active_storage_extensions.rb', line 97

def variant(*args)
  raise Lockbox::Error, "Variant not supported for encrypted files" if Utils.encrypted_options(record, name)
  super
end