lib/lockbox/carrier_wave_extensions.rb in lockbox-0.4.6 vs lib/lockbox/carrier_wave_extensions.rb in lockbox-0.4.7
- old
+ new
@@ -1,30 +1,45 @@
module Lockbox
module CarrierWaveExtensions
def encrypt(**options)
class_eval do
+ # uses same hook as process (before cache)
+ # processing can be disabled, so better to keep separate
before :cache, :encrypt
+ define_singleton_method :lockbox_options do
+ options
+ end
+
def encrypt(file)
+ # safety check
+ # see CarrierWave::Uploader::Cache#cache!
+ raise Lockbox::Error, "Expected files to be equal. Please report an issue." unless file && @file && file == @file
+
+ # processors in CarrierWave move updated file to current_path
+ # however, this causes versions to use the processed file
+ # we only want to change the file for the current version
@file = CarrierWave::SanitizedFile.new(lockbox_notify("encrypt_file") { lockbox.encrypt_io(file) })
end
# TODO safe to memoize?
def read
r = super
lockbox_notify("decrypt_file") { lockbox.decrypt(r) } if r
end
+ # use size of plaintext since read and content type use plaintext
def size
read.bytesize
end
# based on CarrierWave::SanitizedFile#mime_magic_content_type
def content_type
MimeMagic.by_magic(read).try(:type) || "invalid/invalid"
end
+ # disable processing since already processed
def rotate_encryption!
io = Lockbox::IO.new(read)
io.original_filename = file.filename
previous_value = enable_processing
begin
@@ -44,10 +59,12 @@
Utils.build_box(self, options, table, attribute)
end
end
+ # for mounted uploaders, use mounted name
+ # for others, use uploader name
def lockbox_name
if mounted_as
mounted_as.to_s
else
uploader = self
@@ -56,9 +73,11 @@
end
uploader.class.name.sub(/Uploader\z/, "").underscore
end
end
+ # Active Support notifications so it's easier
+ # to see when files are encrypted and decrypted
def lockbox_notify(type)
if defined?(ActiveSupport::Notifications)
name = lockbox_name
# get version