Some context: I am trying to construct a postman request that I will hand off to a mobile developer. The mobile developer will use it as a template for implementing some functionality that involves interfacing with a Rails API.

My question is, what is the most efficient way to do file uploads with a Rails API from native mobile that also leverages middeware to do the conversation of the file object to a Rails object?

Currently I'm doing smth like this:

{"file": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUj...}

...in the request body, and it gets turned into a param in my controller that consumes it. Then I have to decode and write it doing smth like:

    tempfile = Tempfile.new("fileupload")
    tempfile.binmode
    tempfile.write(Base64.decode64("base64 code"))
    tempfile.rewind()

    mime_type = Mime::Type.lookup_by_extension(File.extname('filename.jpg')[1..-1]).to_s
    uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: tempfile, filename: 'filename.jpg', type: mime_type)

This doesn't feel very efficient to me though and I'm wondering if there is a less computational way to do it, ideally in a way that I can get the middleware to handle the decoding/saving/conversation to active storage for me.

0

There are 0 best solutions below