lib/lockbox/utils.rb in lockbox-0.2.3 vs lib/lockbox/utils.rb in lockbox-0.2.4

- old
+ new

@@ -25,7 +25,39 @@ if key.encoding != Encoding::BINARY && key =~ /\A[0-9a-f]{64,128}\z/i key = [key].pack("H*") end key end + + def self.encrypted?(record, name) + !encrypted_options(record, name).nil? + end + + def self.encrypt_attachable(record, name, attachable) + options = encrypted_options(record, name) + box = build_box(record, options, record.class.table_name, name) + + case attachable + when ActiveStorage::Blob + raise NotImplementedError, "Not supported" + when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile + attachable = { + io: StringIO.new(box.encrypt(attachable.read)), + filename: attachable.original_filename, + content_type: attachable.content_type + } + when Hash + attachable = { + io: StringIO.new(box.encrypt(attachable[:io].read)), + filename: attachable[:filename], + content_type: attachable[:content_type] + } + when String + raise NotImplementedError, "Not supported" + else + nil + end + + attachable + end end end