Question about uploading encrypted files

I have a question about something that seems like a pretty standard use case, yet google and StackOverflow aren’t giving any leads.

I’m trying to encrypt a file with the Lockbox gem before uploading it to S3. I have the uploader included in my model like so:

class IdDoc < ApplicationRecord
 belongs_to :user
 validates_presence_of :image
	 
 include IdDocUploader::Attachment(:image)
end

…and I’m overriding the Uploader’s #upload function to encrypt the file like so:

class IdDocUploader < Shrine
 def upload(io, **options)
  key = Lockbox.attribute_key(table: "id_docs", attribute: "image")
  lockbox = Lockbox.new(key: key)
  lockbox.encrypt_io(io)
 end
end

However, doing this results in an error that stacks up to my controller’s create function.

ArgumentError (expected file to be a Shrine::UploadedFile or nil, got #<Lockbox::IO:0x00007f87532f8230 @original_filename="test.jpg", @content_type="image/jpeg">):

Calling super at the end of upload causes a validation error. Has anyone else tried to mix encryption with Shrine, and how did you encrypt your files before uploading them to the S3 cache? Thank you!

You have instructions for Shrine right in the Lockbox readme :wink:

1 Like