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

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!

Related

How to upload files and handle processing and validations - a very general overview?

The problem at hand
I have a rails app.
Users will be uploading files. Anywhere between 1 file to 3000 files. Sometimes they are zip files, and sometimes they are not. I do not want hold up the server with these files uploads, so I am looking for a solution around this problem.
The zipped files will have to be unzipped.
I then want to check whether: the user has previously uploaded the same files? i.e. if the user has already uploaded the same file(2) one week ago, then this is a problem: (i) either we don’t allow that particular file to be uploaded, or we ask the user: are you sure you want to upload the same file again?
Then I want to store the keys/links to the files within the appropriate models/records on the back end.
Was wondering what the best workflow for handling the above could be: i.e a very general overview: in other words, could AWS Lambda / Google cloud computing etc. etc be best employed to handle the above problem? How would we use the Shrine gem, to best handle this situation? Would it make sense to use AWS Lambda rather than using background jobs?
My preferences are to use the Shrine gem for uploading.
My Ideas:
In the client side, the user drags and drops the files the user
wants to upload.
All the files are then uploaded (whether zipped or otherwise) to a temporary bucket location via the Shrine gem.
IF the zip files are uploaded then perhaps an AWS lambda function must be triggered to unzip the files. If that’s the case,then at the end of the day, the keys for these files must somehow be returned to the client, to handle validation issues – but then how would the AWS lambda function be able to return this request to the original client side where the request was originated? Or rather,should the AWS lambda function be generated from the client side,passing in the IDs of the unzipped blobs?
Then we need to run some validations: we want to handle the situation where there are duplicate files. We will need to check with our rails backed as to whether those files have already been uploaded.
After those validation issues are handled, then user submits the form, and all the keys are stored within the appropriate records.
These ideas are by no means prescriptive
Am seeking some very general advise on what the best way is of doing this all. I am by no means constrained to AWS: I could use Google or Azure just as easily. Any guidance on the above would be much appreciated.
Specific questions:
How would the AWS lambda function get triggered?
How would be be able to return the keys of the uploaded files back to the client?
What do I mean by general overview?
Here are some examples of general overviews:
(1) Uploading & Unzipping files to S3 through Rails hosted on Heroku?
(2) https://www.quora.com/How-do-I-extract-large-zip-files-in-AWS-Lambda
Any pointers in the right direction would be much appreciated.
Cheers!
This isn't a really difficult problem to solve if you are willing to change the process flow a little bit.
In the client side, the user drags and drops the files the user wants to upload.
When the user requests the upload operation to begin you can make HTTP GET requests to an API Gateway endpoint, backed with a Lambda. The Lambda can query for previous files uploaded by the client and send back a result set showing what files already exist. You then filter those out and send only what is considered new from the client to the server. This will save the user time in waiting for the upload to happen and save you time on the S3/Lambda side of not having to store duplicates or process them. This isn't a substitute for server-side validation though, you'll still want to do that. For legit clients, this will save you and them a lot of bandwidth and storage.
All the files are then uploaded (whether zipped or otherwise) to a temporary bucket location via the Shrine gem.
This works. As they enter the temp bucket, use a Lambda with an S3 event to process the files, unzip files, push any metadata needed into DynamoDb and delete the files from the temp bucket. In the temp bucket, I would place the files into a folder that is unique per request and user. I would take the user/client Id and a UUID of some kind and make that your folder name. Such as Johnathon+3b5339b8-c8db-4d5c-b678-406fcf073f4f, or encode this value into a Base64 string and make that your folder name. Store this in DynamoDb with each file uploaded into your permanent bucket with the Hash Key being the userid/clientid, a Sort Key being the full folder path + file name and an extra attribute of IsProcessed. The IsProcessed attribute will be updated by your Lambda that is processing the files and moving them to their permanent S3 bucket. If there are errors, you can put the error in this field. If it is successful then you put it in this field.
the keys for these files must somehow be returned to the client, to handle validation issues – but then how would the AWS lambda function be able to return this request to the original client side where the request was originated? Or rather,should the AWS lambda function be generated from the client side,passing in the IDs of the unzipped blobs?
The original API request to push the files to the temp S3 bucket would be able to return back to the client the folder name johnathon+3b5339b8-c8db-4d5c-b678-406fcf073f4f to the client. So let's say you made a HTTP POST to /jobs. You would return back 201 Created with a HTTP Header of Location /jobs/johnathon+3b5339b8-c8db-4d5c-b678-406fcf073f4f. Your client can then start polling /jobs/johnathon+3b5339b8-c8db-4d5c-b678-406fcf073f4f for the status of the process.
Your response back to /jobs/johnathon+3b5339b8-c8db-4d5c-b678-406fcf073f4f can return the DynamoDB records. This would include all DynamoDB records for the HashKey matching the folder name. Your client side can look at all of the objects in the result set and check the IsProcessed attribute to see if everything worked out ok, or if there were issues.
Then we need to run some validations: we want to handle the situation where there are duplicate files. We will need to check with our rails backed as to whether those files have already been uploaded.
Handle this with the Lambda that is executed by the temporary bucket. Grab the files from the temp bucket folder, handle your business logic and back-end queries then push them to their final permanent bucket.
After those validation issues are handled, then user submits the form, and all the keys are stored within the appropriate records.
All of this would happen asynchronously, starting when the user submits the form. The client side needs to be able to handle this by making HTTP GET requests to the endpoint mentioned above, checking for the status of the process. This gives you some more flexibility as you can also publish SNS messages on failures as well, such as sending an email to the clients if they upload 3,000 files and you need to spend 30 minutes processing them.

Using Google's text to speech API with Hyperstack

I would like to use the Google text-speech API to let my user type text into a text control, and then click a button which would send the text to my Rails server, where it would use the Google TextToSpeach API to create an mp3 file of that speech.
The Google API looks very simple to use: https://cloud.google.com/text-to-speech/docs/create-audio
In a traditional Rails application, I would write an API to pass the text to be transcribed and would expect that API call to return the path to the MP3 file created for the user to download.
It seems that a Hyperstack Isomorphic Operations would be the right approach for this, but how do I ensure the operation only runs on the server and not on the client though and how do I get the output value of the Operation (ie the file created) so I can display it in the browser for the user to download?
I should stress that I only need the Google API to Create the Audio file on the server (not play it). The user will then download the created file so their own use.
An operation is not going to help you here. Why?
Because unless you know a trick I don't, the only way to play an audio file without a lot of extra work, is to point an HTML audio tag's source at a url on the server.
This is then very easily done by a standard rails controller method that decodes the string from the URL params and returns the mp3 file in the response body.
For example /utils/text2speech.mp3?text=Hello%020There would just return the MP3 file.
Just to fully answer your question however, the Hyperstack::ServerOp class is a subclass of Operation that only runs on the server, but can be called from the client.
Too bad its no help here :-)

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à.

How to stream an audio file without a direct link

I have a rails web app sitting on an nginx web server. I have some audio files on my server, and I want people to be able to listen to them, and to be able to seek to any part of the audio file to listen from that point.
Simple right?
I was using direct links in the src of my html5 audio element. It worked great. The file could be played and seeking worked.
Enter security and auditability.
My audio files are sensitive. I only want certain people to be able to listen to them. I also need to know each time that they listen to them. Suddenly the public directory isn't going to work.
Enter rails's send_file.
Send_file initially appeared to be exactly what I needed. It allows rails to serve my audio files, and I could keep my files in a protected directory, I could check the current user's permissions, and I could create the appropriate audit trail. Except...
With send_file I can't seek. That is a deal breaker.
A few stackoverflow questions address getting send_file to handle http-range/byte-range requests. The ones I reviewed are:
what is the proper way to serve mp4 files through rails...
rails media file stream accept byte range request through send file ...
After doing more research, I found the following blog post:
https://blog.echoplex.us/2014/08/19/so-you-want-to-stream-videoaudio-with-rails/
tl;dr
don't use rails send_file to serve media. don't try to make it like the stackoverflow questions say you can. Instead, use nginx and X-Accel-Redirect, and end up with a request pipeline that looks like you->nginx->rails->nginx->you
I am considering taking his approach, but didn't know if there was a better way to do this.
What are my options?
(also, you can assume that I'm using the current versions of rails and nginx)
DON'T USE sendfile please. Use X-Accel-Redirect or the advice below.
Nginx secure_link module helps you to serve files straight from disk with private links. No backend required. The full example is here.

Receive video files through http on my rails JSON api

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.

Resources