Rails 4 - Paperclip - Loading Image Using URI URI

Here's my question, I am using Rails 4 and paperclip to handle uploading images to my amazon s3 server. However, I want to allow the user to use their own webcam to take a picture instead of having to select an existing file on their hard drive.

When I take an image, I get the image data URI as a result :

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAFACAYAAADNkKWqAAAgAElEQVR4nDS7RXNj6bpuW/98xzkRZ8Paa62irMx0ZpptMTOzNMXMzAyWzGnNOW5DdRsOufFJzREPvb/wmkN6KiM9lhCf60gvbaTnJryUkF6LSE8FpLc00nME8SmF+FSEfQppHUF8LMA6i7SqIh36SA8FpHUUHlJIuwLsKkjzBGwCiNsAx6kDFkbEhQFx7EOcCEjrCNIsiDgKwFMZHrJIyzSsikjTKtJ8COs+H/M40tyEtAud3kwrSJsmx2EEaRFCnMVP31vGYGFHWl0ibe6Q9mHEcRpmRaRVDWkRg53A+yzI28ADDwXEWRhxZkRa+GGd5ufQizQLcpwKsC3DqoA0zSKN84jDLNI4wUfPiTR0w9yGNLYhTSOIkwofozzHeYqnoYOPmQ9pKiCOUjAt8d7OIc7CvPQdLMsqNjU1bwMnT10Pm6oPcZTivelkU5bx3FLwWLrlqXzJR+UcsSXnpani0FTzWFXy3DBxqMV46wq8tG3s8gr2RTPHZpxjNcax7ua9YuNQ0PNaM/He9PJUj/DaSPNU8/NStvNY9vBWcfNYMPBU1vBaM/NQMPFYsrNKaThUbOyqHhYFE5OMiklKyaZgZJYysipa2Nf8TJMeNsUA04KCSfYzg8QnVoVbVtkfTEP/YJv+k33uC5vUX8yFL+wKd4ziX5mk/6If/0I/fsM8b2AoKBnGTPQSDkYpFwPBwDStYVvVMcpc0o1c0BcUdGMaBnENVf8Z7fAZy6yKmaBkmpCzTt2zTHxmI1yyS2jZJdy85EJMo1YGUQ0TQcs0amKT9NKLKOgJl7SFc+rCJd2Mi27CRyfpo532U015KCbstNIWWuFresIdPUFLP+6gGbij7PxEP2qi4bAwCUfphRN0Qn46gp5+7IZe/IZeVMks4mPk9TCLuGmH9HSiZto+AwOfm14wRjcUoRH0UA2aybvkDDJmujEDNa+MbkhDx6+kFTDRibioh0yUg3JqYT15j4aqz0TeJSPvPSfv/UoroqQZ0lEPKmlELsn5/pNq4P/QCf0/Wt7f6Xr0dPxmWl4tBfsnMo7fyHjOyDn/Im35nWbknqxXTsFnpBt3UHHe0/Bekrf/k4LzK82AjIr7jGbgG1WfhozbRtLnI2EzUvbekzf9QcH8nZrzmpb7hpL7G0nHn+ScZxRt38kZP1F3fqfsvKDkuCduOCdrOads+0rJ8hcF8xkVxzVp0xkx3Tfylu8UjP9FVv8f5B1XJIwKyk4tWccVUf2/iBn+Tcz0mW7MTMZ8Rkr1n5RU/6BqOCetPiNm+I2g6v8S1/0XKeOvFIx/Ebv8D/KG/yVn/hdJ9T

      

My question is, how to load this image based on this data URI using paperclip in my image controller?

Is there a way to pass this DATA URI directly to my controller and the paperclip will take care of that? Or do I need to create and write a temporary file to restore the image and then pipe it to paperclip?

Thank you so much!

+3


source to share


1 answer


Paperclip#io_adapters.for

will try to find the correct adapter for the job, in this case Paperclip::DataUriAdapter

. You can also use Paperclip::DataUriAdapter

directly, of course.

def save_data_uri_to_attachment(data_uri)    

  # Instantiates Paperclip::DataUriAdapter attachment
  file = Paperclip.io_adapters.for(data_uri)
  file.original_filename = name

  # Save the model  
  model.attachment = file
  model.save!
end

      



Source: fooobar.com/questions/1565811 / ...

+3


source