Uploading a file the Rails way looks like this in console:
Started POST "/onboard/add_doc" for 2600:1700:ba00:3970:1900:ab4c:b842:8b5c at 2021-03-13 12:42:47 -0800
Processing by Users::DocsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "doc"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007fe0fef3de78 u/tempfile=#<Tempfile:/var/folders/hj/g_90jx_n7s58m73qlf9h0c4w0000gn/T/RackMultipart20210313-97062-1pd54fh.jpg>, u/original_filename="Gil-Next-Door-Gil-and-Wolly.jpg", u/content_type="image/jpeg", u/headers="Content-Disposition: form-data; name=\"doc[image]\"; filename=\"Gil-Next-Door-Gil-and-Wolly.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload"}
However I am not doing that. A js library called Uppy handles file selection and upload, and inserts the JSON into a hidden form field. When this form is submitted, it is not automatically converted into a ActionDispatch::Http::UploadedFile
object. I'm not entirely sure why, so I handle that in the controller:
def create
image = params.require(:doc).fetch(:image)
file = StringIO.new(image)
lockbox = Lockbox.new(key: Lockbox.attribute_key(table: "docs", attribute: "image"))
encrypted_image = lockbox.encrypt_io(file)
@doc = Doc.new(user_id: current_user.id, image: encrypted_image, mime_type: mime_type)
@doc.save
end
Console output looks like this:
Processing by Users::IdDocsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "doc"=>{"image"=>"{\"id\":\"6536f20f163a67f5b432d250351eac7c.jpg\",\"storage\":\"cache\",\"metadata\":{\"size\":55368,\"filename\":\"cam-1615814031784.jpg\",\"mime_type\":\"image/jpeg\"}}"}, "commit"=>"Upload"}
That output is fine (I think), but the file is not properly encrypting before upload. I'm getting blank white squares when I view them, and Lockbox needs an ActionDispatch::Http::UploadedFile
object. Is my uploaded_file
definition missing something? How can I access uploaded_file
attributes like headers
to make sure its being submitted correctly?