Using Activestorage (direct uploads) via API - ruby-on-rails

I'd like to use Rails as a backend to a mobile app (Android and maybe iOS), and one of the requirements is file upload.
I haven't been able to find any resources on using Activestorage with direct upload in this way – does anyone have an example or tips?
The alternative – I suppose – is to reverse-engineer activestorage.js and do exactly what it does. But perhaps I'm not the first one with this need...

Rails does not care if your data is sent from a mobile-app, html-form, react-app, server-to-server...
If you can authenticate the request and send a multi-part file, then you do active-storage from your app.
Probably smart to look at gems/apps that do this already https://github.com/cbothner/react-activestorage-provider

Related

Rails: receive and send images

I'm writing rails application. My task is to show data from old java application. One of the objects has images attached, I have to display these images and allow user to add new ones.
I want to know what is the best way to let user uploading images to old app.
Old app has http post method for adding images with params: multipart file, md5 hash of the image and object id.
I tried using paperclip or carrierwave but their documentation is about saving uploaded images and I just want to transfer them somewhere else and not save anything.
there are few ways to communicate with java from ruby.
REST API - expose Java functionalities through endpoints. then use an HTTP client.
Remote Procedure Call - Apache Thrift
JRuby - embedded java programme into Ruby
however, I think maybe you can just proxy the Java API without going through the rails application.

Is it possible to send file through actioncable ? If possible, how?

I already implemented the chat function using actioncable but don't know how to send file through it or is it even possible or not.
I am trying to make chat application where user can upload file in chatroom other users can see that immediately without reloading the page as actioncable offers.
It's possible to upload files using websockets. (File upload using java websocket API and Javascript)
But through ActionCable that's not possible at the moment. As ActionCable wraps the Websocket in Javascript this is also going to be "hacky" to patch this in, so I would wait for a new release and write an issue on the rails repo instead.
So for your chat app you still need to use a normal form submit to upload a file. If you want it to happen asynchronously you can use my "patched" version of jquery-ujs which allows sending files with the "data-remote=true" flag.
See https://github.com/Elektron1c97/jquery-ujs-files
It's possible by transforming your file into a base64 data url on the client via some js then to send this url thru actioncable to the server who's gonna broadcast just a base64 data url.
I have test it with image, audio and video files. Not with pdf or txt but it should work. For big file like a video it's instable.
It's just experimental.
Voilà.

Upload big file on Rails-API

I wonder how manage on a rails-api the upload of a "big" file (arround 100m). Is it possible to stream to s3 the rack tempfile during the upload?
I'm creating a new service how just need to receive post request with parameters and files ask the original app if it's everything is ok, and process them. Lot's people over blogs tell that ruby is not the best language for that, it's not a problem to change.
So I would like to know if making a rails api who will received post, return status, and communicate with the other rails app is a good thing.
Thanks in advance
Yes it's possible. Amazon has their own AWS SDK Gem which provides the s3 functionality.
s3 = Aws::S3::Client.new
Here's how to get started using it.
This question however is a little off-topic because you show no proof of trial and error.

Using an API key in Rails App

I just got an API key for a database I wish to access and want to start building my Rails app. However I dont know where to begin with the API key. Specifically I want to use the brewerydb data and I am building an app where users can find the closest brewery to their location. Can anyone tell me how to get started? I am new to Rails and have never used an API before. I don't know where to begin. What file should I put it in, etc... I know I should probably update the GEMFILE, where else?
Thanks!
Check the documentation for the API. That's all I can say. (well... not really: )
Most API's rely on REST or SOAP, which is basically making HTTP request to certain URI's. An example may be
http://api.somewebsite.com/beers.json
Which would return, for instance, a JSON array of certain beers and their properties.
Furthermore, more often than not, you can test API's (that do not require certain HTTP headers for authentication, which makes it harder) by manually constructing the URI's and opening them in your browser. This way, you can verify that your request is okay before you try it in your Rails application an cannot figure out why it's not working.
You should use the 'figaro' gem https://github.com/laserlemon/figaro
It creates a "application.yml" file in which you can add your API key.

Posting multipart form data with Ruby

I'm building a rails app that interacts with a 3rd party API
When a user uploads a file to rails, it should be forwarded on to the 3rd party site via an HTTP POST.
In some cases, the upload can be several hundred MBs.
At the moment, I've just been re-posting to the API using Net::HTTP and accessing the multipart form object like so
#tempfile = params[:video][:file_upload].tempfile
This is hella slow though and feels kinda dirty.
Is there a better way to do this?
Is it possible to have the user post directly to the 3rd party service or do you have to handle the API through your Rails stack? Ideally you would be able to do this and would not have to load the file into your stack and then re-post it to the API. If you can't post directly, I would recommend seeing if the API has a streaming service so that you can send parts of the file instead of the entire thing at once. Either way I think you'll start running into Timeout errors on your side and on the API side with large files, so you'll have to increase your own timeouts or create a different type of streaming file uploader.
Spin up a background job using DelayedJob. In the delayed job, you could try rails redirect_to.
https://github.com/tobi/delayed_job
http://apidock.com/rails/ActionController/Base/redirect_to

Resources