Rails send_data() send_file() problem - ruby-on-rails

I have a paperclip attachment in one model, but I`m not saving the file in /public, but /assets. And when the user what to open the file I use the send_data() function, which makes the user to download the file.
My question is how can I show the file in other way (not nessecery to download)? So if the file is a image, I will see it directly in the browser without download.
Thanks!

Try:
send_data foo, :disposition => 'inline'
This would tell the browser to just render the content, instead of prompting the user to save it.
… from http://apidock.com/rails/ActionController/Streaming/send_data

Related

Rails: Refile for documents to download

Using the refile gem, I have uploaded documents (.pdf, .docx, .pptx, etc.). Uploading is fine. When I use attachemnt_url, it produces something like /attachments/...234jksdf2.../document. When I click the link_to, it downloads the document without an extension.
What's happening to make it operate this way? How can I restore my file type sanity?
I was trying to address the exact same issue, this is one approach I tried:
Refile allows you to save additional metadata such as the content_type: https://github.com/refile/refile#additional-metadata. The resulting file content type will be saved as something like "image/png" or "application/pdf".
We can then apply something like
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
Whereby
in document.rb
def file_extension
file_content_type.split("/").last.to_sym
end
The only issue is that this doesn't automatically downloads the file, but rather opens it in a new page where you can then download the file. Still looking for better alternatives!
This is what ended up being the correct solution for me.
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
def file_extension
require 'rack/mime'
Rack::Mime::MIME_TYPES.invert[document_content_type].split('.').last
end

How to download image from url and display in view

I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.

Carrierwave how to get the file extension

I'm developing a Ruby on Rails application that requires file uploading/downloading. For the upload part i used the gem carrierwave since it's very easy to use and flexible. The problem is: once i uploaded the file, i need to know a few things: i.e. if it's a pdf instead of downloading the file i show it inline,and the same goes for an image. How do i get the file extension and how can i do it to send the file to a user?? Any feedback is appreciated Thanks!!
Determine file extension (I suppose a name for mounted uploader is 'file'):
file = my_model.file.url
extension = my_model.file.file.extension.downcase
Then prepare mime and disposition vars:
disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type
if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
disposition = 'inline'
end
(add other extensions if you want).
And then send the file:
send_file file, :type => mime, :disposition => disposition
Once you have uploaded a file, the name is stored in the database. This also includes the extension. Assuming you have a User model with an uploader mounted as asset, then you can get it as:
user.asset.file.extension
As for sending it to the user, if you call user.asset_url, it will give you the URL where the file is uploaded. The user can use that link to get the file. Or am I misunderstanding what you mean by "send the file to a user"?

How to generate a text file from a String for download in Ruby on Rails

I'm building a website using Rails and I have a Model with some text type value store in MySQL database, I need to provide a download link for my users to download a "*.txt" file which contains those texts.
what I've tried is to use render :text => my_text but it's kind of ugly and the browser can't start a download .
Not I'm trying to use CarrierWave and mount a output_file to my model , I want to build a method to generate a file from its text value. Any suggestion would be greatly appreciated. Thanks!
send_data 'text to send', :filename => 'some.txt'
Documentation Here

How to change filename prompt text browser Save As dialog?

In my web page (rendered by Rails), I'd like to let the user right-click on a photo to bring up the browser's Save As dialog, to let the user save the photo to their hard drive.
However, the photos on my server have unusual filenames (long hex names) with no file extension. The filename prompt in the Save As dialog has this ugly filename. If the user hits save, they'll end up with a poorly-named file, with no file extension.
The web page is aware of the photo's real file name (the name that came off the camera, for example). Is there a way for me to programmatically override the Save As dialog's filename prompt with a filename of my choosing?
I'm aware of the Content-Dispostion header, and that via this header a filename can be specified. However, I think that in order to be able to make use of this header, I need to load/render the entire file to the browser. If the asset to be made available for download is a movie, that loading of the file could timeout the browser...like, if it's a 100meg video.
Thoughts?
-A
I think I understand the problem here because I encountered (and resolved) at least part of it myself not too long ago.
I have some large mp3's and I link to them on my website
A few problems
I needed to set my content-disposition header to attachment in order to prevent files from automatically streaming whenever a user clicked the download button
my files are on a remote server
my files are large (100MB)
large files can tie up rails controllers if not handled properly
Now, Michael Koziarsky advises in this article that the best way to keep your rails processes free when serving large files, is to create a download action in your controller, and the do something like this (note the use of x_sendfile=>true):
def download
send_file '/path/to/podcast.mp3', :type => 'application/octet-stream', :disposition => 'attachment', :filename=>'something.mp3', :x_sendfile=>true
end
:x_sendfile tells apache to let the file through without tying up a rails controller process. The rest of the code sets the filename and the content-disposition header.
Great, but I'm on heroku, like everyone else nowadays. So I can't use x_sendfile.
I found that I couldn't modify the nginx configuration file either as it's locked down by heroku so it was not possible to get x-accel-redirect (nginx equivalent of x-sendfile) working
So, I decided to add a perl script (see below) to the cgi-bin on our asset-host and this script sets the content-disposition to attachment and gives our file a name too.
Instead of doing a restful download like this:
link_to "download", download_podcast_path(#podcast.mp3)
we just link to the mp3 making sure that we go in through the cgi-bin so that the perl script gets called on every mp3 that leaves the server
# I'm using haml
%a{:href=>"http://afmpodcast.com/cgi-bin/download.cgi?ID=#{#podcast.mp3}"}
download
The result is that my rails controller is no longer called into action when someone downloads a file
I found the perl script here and chopped it up a bit to work for me:
#!/usr/local/bin/perl -wT
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
my $files_location;
my $ID;
my #fileholder;
$files_location = "../";
$ID = param('ID');
open(DLFILE, "<$files_location/$ID") || Error('open', 'file');
#fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');
print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print #fileholder
My code, is on github but you'll likely have all sorts of problems using it on your machine as i make heavy use of ENV variables that I store in bashrc and I have no documentation or tests ^hides^
You could do some smart server side url rewrite, like for example rewriting foo.mpeg to youveryuglyfilenamewithoutextension.
Set the Content-Disposition to "attachment; filename="...that's fine. "attachment" explicitly means it's not to be rendered in the browser, file renaming works nonetheless (or possibly particularly for that case).
Based on your comments, you have a few problems.
You want to set the filename using your Rails app.
The file is on a remote host and your Rails app is acting as a middleman.
The file might be big, so you want the file to be sent out to the browser as you receive it instead of queuing the whole thing.
Streaming only with Rails is tricky for a few reasons.
You would need an HTTP client that lets you access the message body as you receive data instead of blocking until you have everything. Net::HTTP is not that client. I'm not sure what library would be better suited.
Once you have a more event-driven way to get your file in pieces, you can pass a proc to the render:
render :text => proc { |response, output| ... }
output can be used like an IO object. Some servers may buffer before sending anyway, though, so that's something to look out for.
It would be easier not handle the byte-shuffling in Rails.
If your webserver or the proxy in front of your webserver supports the X-REPROXY-URL HTTP header, your application can set that header and your webserver or proxy will stream the file.
Perlbal is the only proxy server I know of that supports that header out of the box.
An Apache2 module is also available.

Resources