Receive video files through http on my rails JSON api - ruby-on-rails

So here's the thing. I believe my case is pretty particular at this point, and some help from the experts it's highly advisable.
I have an API built on Rails (3.2.6) and what I want to able to do is receive a video file (mostly .mp4, .avi) and upload it to s3 through a Process Queue (Using Resque).
Now I'm kind of lost on how to do this. To my understanding, I would be receiving a byte[] (Array of bytes) through the request which is the video and send that as a param to my Resque job in order to upload it (Resque Job params can only be strings, not objects)?
Has anyone had any experience doing this sort of procedure. We're pretty much trying to mimic the http://docs.brightcove.com/en/media/ create_video method. Where a Video object can be created either by sending the direct file in the request or the link the file....
Any suggestions?

Try using the CarrierWave gem. You should allow someone to HTTP POST the file data to your API, and then save that file data on the backend and upload it to S3 using CarrierWave.

Related

On Heroku, can I use Rails to serve a generated audio file back to my React Native front-end?

I'm generating temporary audio files using Rails and storing them in the tmp directory in Heroku. I think want to send the audio back to Expo/React Native to be played. How can I use Rails to serve that data as a POST response?
In my Rails API backend, I connect to a Text-to-Speech API (Google), and generate an mp3 from text. I can verify that these files are being correctly created and stored in tmp as per my POST request. I am missing the conceptual piece of how to return them back.
It's ideal for me that these files are temporary, as I only need them during a single session. I can see a couple of options.
I can save the files as .mp3s in tmp, as I'm currently doing, and find a way to attach them to my POST response.
I get the audio back from the API as binary, so I can skip saving the files as .mp3s, and respond to the POST with the binary as JSON, or something.
???
Play sound in Expo/ React Native
I'm trying to avoid setting up an AWS bucket for this if possible, but if that's the best way to do it, I'm happy to hear that as well.
Thanks!

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.

fb_graph upload photo from base64 encoded string

I'm receiving attachments from postmarkapp (described here: http://developer.postmarkapp.com/developer-inbound-parse.html#attachments).
I want to upload those photos to facebook using fb_graph (https://github.com/nov/fb_graph) using its photo! method (https://github.com/nov/fb_graph/wiki/Photo-and-Album).
This is easy and works fine when testing by specifying a :source like in the examples from an actual file.
However I'm trying to not write out to a file but instead just convert the base64 encoded string to StringIO and pass that as the :source argument. This doesn't work and I get this error:
ruby
FbGraph::InvalidRequest: OAuthException :: (#324) Requires upload file
The reason I don't want to write out a file is because I'm using heroku and delayed_job so I'm not sure if a file I write out will still be around when the job is processed. That would be nice however since my current plan is to store the images in the db with delayed job.
Thanks.
I couldn't find a way to make this work with heroku without first uploading it to mongohq with gridfs. You can't use the cedar ephemeral file system because those files, written during a controller action, won't be visible to your delayed_job worker.
So even though it sucks I do this;
Receive upload from postmarkapp (blocks one dyno)
Write to mongohq using grid fs (this involves one upload which blocks one dyno)
Queue job using delayed_job
Read back from mongohq (blocks one worker while downloading)
Re-upload to FB when posting
So why not just post directly to facebook if I'm going to incur that initial blocking cost of uploading to mongohq anyway? Because that upload is way faster than uploading to FB for reasons unkonwn.
The right answer on heroku is to have a node.js dyno handling these callbacks from postmark so the dyno isn't blocked during either the read from postmark or the write to mongohq (or facebook) then to do some extra work to have the node app interact with the rails app to stay synced.

Amazon S3 Multipart Upload with plupload and Rails 3

Amazon has multipart upload functionality where you can send a file in chunks and get it assembled on S3. This allows for some nice resume like functionality for uploading to S3. From another question i got this nice link: Rails 3 & Plupload
My question is does anyone have any examples where they used the plupload chunking feature with the Amazon multipart feature? Ideally with carrierwave & fog.
I can see it doing the following:
Generate Unique ID for the upload with plupload,
can we do an event when the plupload
starts?
Attaching an ajax request to
the chunk completed with the ID
Having ajax controller method on the
server which uploads to s3 using the
ID
when all are complete fire a
controller action to reassemble
There is supposedly some PHP code which does some combining, but not with S3 and i can't stand to read PHP.
this is very similar, you should find Interesting
enjoy & feel free to fork/pull request ... & so on
You can find simple core java code without AWS library, This will help u implement in any technology..
http://dextercoder.blogspot.in/2012/02/multipart-upload-to-amazon-s3-in-three.html

Resources