1

I'm encrypting my Shrine file uploads in a controller action, and need some way of validating their mime types before doing anything else. Shrine's validation plugins are great, but they do not work on encrypted files. I need to come up with a way around this.

Suggestions here are for ActiveStorage, and seem off. The first post suggests validating in the model, but I need to do it in the controller. Possibly something like...

def create
 image = params.require(:id_doc).fetch(:image)
 respond_to do |format|
  if image.content_type == 'image/jpeg' || image.content_type == 'image/png' && image.size <= 3.megabytes
   #encrypt file and save
   format.html { redirect_to root_path }
  else
   format.html { redirect_to onboard_queued_path }
 end
end

Even then it seems insecure. I could just change file names. This answer seems promising. Alternately I could use this but I would prefer not to install an entire gem.

calyxofheld
  • 1,538
  • 3
  • 24
  • 62

1 Answers1

1

The answer was in the docs.

Since I want to determine the mime type before passing the file off to anything else, I simply do Shrine.mime_type(image) in the controller.

calyxofheld
  • 1,538
  • 3
  • 24
  • 62