1

Been having some difficulty with an attachment issue on my site. At the moment our iOS app is pointing at an API endpoint for attachments, and sending a request similar to this:

POST /api/v2/attachments HTTP/1.1

--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="attachment"; filename="attachment.jpg"
Content-Type: image/jpeg

...image data...
--Boundary+0xAbCdEfGbOuNdArY--

Now, the request succeeds and the image is in fact uploaded, but it's turning out to be an invalid image because the boundary data is written to file. It basically looks like this:

file = Tempfile.new('attachment')
attachment_data = request.body.read
attachment_data.force_encoding('UTF-8')
file << attachment_data

attachment.asset = file
attachment.save!

Obviously request.body.read is including the entire request, Boundaries and all. We do actually have a stripping method that runs through each line of the file and strips out non-image data, but that's obviously not performant at all.

In an ideal world, we would just be getting the image data itself and using that to populate the tempfile, but I'm afraid I'm completely stumped about the best way to go about that.

Thoughts welcome. Thank you!

mchitten
  • 392
  • 2
  • 9
  • rails provides default mechanisms to handle file-uploads. have a look at this example: http://stackoverflow.com/questions/11401553/curl-request-to-upload-image-from-local-machine-to-ruby-on-rails-application – phoet Jan 29 '14 at 03:29
  • Thanks @phoet. Unfortunately we're dealing with some legacy code on the iOS side and there's not a lot we can do to change the actual request until their rewrite is done. Is there anything we can do in the interim? – mchitten Jan 29 '14 at 14:31
  • i did not fully understand your issue. is it about performance? i don't think that there is anything you can do. at some point you will have to handle the payload. – phoet Jan 29 '14 at 16:09

0 Answers0