undefined local variable 'image' for Class

102 views Asked by At

I would like to encrypt a file before storing it on S3. I'm using Shrine for uploads, and Lockbox for encryption. Shrine#upload takes an IO-like object, but the Lockbox documention excludes some steps that are needed for setting this up. It simply says to do something like:

class Photo < ApplicationRecord
 belongs_to :imageable, polymorphic: true
 validates_presence_of :image

 lockbox = Lockbox.new(key: ENV['LOCKBOX_MASTER_KEY'])
 PhotoUploader.upload(lockbox.encrypt_io(image), :store)
end

In this case, image is the undefined local variable.

If the file is user uploaded (and whitelisted in the controller), how would I access it in the model so that I can perform this operation when it is saved?

1

There are 1 answers

0
Rockwell Rice On

You could move this into a before_save method. You could also change it to after_save if you wanted to do it then (it was a little unclear from your question).

before_save :encyrpt_image

def encrypt_image
  lockbox = Lockbox.new(key: ENV['LOCKBOX_MASTER_KEY'])
  PhotoUploader.upload(lockbox.encrypt_io(self.image), :store)
end