Hosting MP3 files in rails without auto downloading them - ruby-on-rails

Im building an API and I need to host MP3 files.
I have used active storage to upload the MP3 files and that all works fine, however when I create an endpoint with a url to the MP3 file, that URL just downloads the MP3 file rather than taking me to a page where I can listen to it.
Im trying to replicate the behaviour seen here:
MP3 link
Does anyone know how I can achieve this in rails?
Any help is much appreciated.

When you use send_file without a type, it will download the file raw.
Instead, you must specify the type of file so the browser knows how to handle it. For example:
def download
send_file "/path/to/file.mp3", type: "audio/mpeg", filename: "file.mp3"
end

Related

How to download pdf automatically with an url

I am using Ruby2.6.0, Rails5.2.3 and Kibana6.6.1.
I need some help to realise pdf download automatically. Is there any way to download pdf/png automatically using the following url in controller? 'automatically' means users won't feel the file download and don't need to click download icon.
http://localhost:5601/api/reporting/jobs/download/jvhs9dga06559d006260ms70
Edit:
Add the code I tried. It helps me to download a file. But I cannot open it with Chrome or sublime. It seems like the file is related to kibana Chromium sandbox (https://www.elastic.co/guide/en/kibana/current/reporting-chromium-sandbox.html). Is anybody know what type of this download file is and how to open it? How to save it as pdf/png file in my local folder?
File.open('./public/testfile.png', "wb") do |file|
file.write open("http://localhost:5601/api/reporting/jobs/download/jvhs9dga06559d006260ms70")
end

Rails s3_direct_upload without file field

My website generates a file in javascript (audio recording) and I then want it to be uploaded to Amazon S3.
I first managed to get the uploading part working by sending the generated file to my server, where it is uploaded. However I would like now to upload the file directly to S3, without going through my server.
So I started to use the s3_direct_upload gem, which works great when using a file_field. However my file is generated by the javascript and :
- The value of a file field has to be set by the user for security reasons
- I do not want the user to have to interact with the upload
I tried to play with the S3Uploader class and to directly add data, without any success for now, it seems that I do not use the correct method.
Does anyone has any idea on how to achieve S3 direct upload without a file field ?
Thanks
Never mind, I found out that the S3Uploader class used by the s3_direct_upload gem has the same methods as the jQuery-File-Upload from which it is derived.
So one can call $("#s3_uploader").fileupload('send', {files: [f]});
And the f File will be uploaded to S3 directly

Extract metadata without uploading

I hava a Ruby on Rails application that works with video playlists. Now I would like to extract timecode information from the video file without uploading it to the server (takes to long). Is the possible?
If it is not possible, is there a way to export te metadata locally and uploads these xmls (for example) to the server?
Thanks in advance
afaik, this is not possible in browsers that do not support File API. Take a look at jquery file upload and the way it allows user to identify the file extension before upload.

rails as proxy for remote file download

I am having a rails application on e.g. example.com . I am using a cloud storage provider for any kind of files (videos, images, ...).
No I would like to make them available for download without exposing the url of the actual storage location.
So I was thinking of a kind of proxy. A simple controller which could look like this :
data = open(params[:file])
filename = "#{RAILS_ROOT}/tmp/my_temp_file"
File.open(filename, 'r+') do |f|
f.write data.read
end
send_file filename, ...options...
( code taken from a link ).
Point being is that I would have to download the file first.
So I was wondering if it would be possible to stream the file right away without downloading from the cloud storage first.
best
philip
I was working on this exact issue a while ago and came to the conclusion that this would not be possible without having to download the file to your server and then pass it on to the client as you say.
I'd recommend generating a signed, expiring download link that you insert into a hidden iframe whenever a user clicks a download link on your page. In this way they will get the experience of downloading from your page, without the file making an unnecessary roundtrip to your server.

Carrierwave + Fog(s3). Letting the users to download the file

I am working on a project where I have to provide download links to user for the files which are stored in the s3. Initially I tried,
link_to "Download", #medium.file.url
But this opens the file directly on the browser. When i tried to download an mp4 file, chrome started playing it automatically. I don't want that to happen. So I am using send_file for this task,
def download
#medium = Medium.find(params[:id])
send_file #medium.file.url
end
In my local, I have set the storage to file and I have tested this, which works perfectly fine. But on staging, the files are served from s3, I am always getting ActionController::MissingFile. My app is hosted on heroku. I also want to know if using send_file is good choice or if there is a better way of doing this.
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
When i was googling, i found that nginx header setting for sending files should be enabled for production. I added the following line in config/environments/production.rb. Still no luck. I need some help on this. Thanks.
I think this is because S3 wants the file to be played.
I don't have access to my S3 here, but I solved the problem by changing the action for PDF files (which was shown my case) in the S3 control panel.
If you can fix this, I think you can avoid using send_file completely.
Edit: I managed to access S3 now. Go to properties -> Metadata, and add the key
Content-Disposition: Attachment
Then your file will always be downloaded instead of shown.

Resources