Storing a file download in a temp file

In my app, I have an URL to download a file from a remote site , when
using this URL in a browser I download the file…

http://www.anotherdomain.com/resource/download/579633

but I need to to get the content of this file within my app , as I
must encode it and transfer to another app

if I use this url with open-uri, I get the html page, not the file
itself…
should I execute the download within my app ( how) and store it in a
temp file
then open it and encode …

how should I proceed to make it as simple as possible ?

thanks for your feedback

Kad K. wrote:

In my app, I have an URL to download a file from a remote site , when
using this URL in a browser I download the file…

http://www.anotherdomain.com/resource/download/579633

but I need to to get the content of this file within my app , as I
must encode it and transfer to another app

if I use this url with open-uri, I get the html page, not the file
itself…

I’m not following you.

If the above link is a URI representing the file (with the appropriate
content-type) then open-uri should get the file. Of course, if that URI
represents an HTML page then yes, you would get an HTML page.

HTTP is content agnostic. It will simple GET (or POST, PUT, DELETE,
etc.) whatever the URI represents.

OpenURI is just a wrapper around net/http, net/https & net/ftp:
http://www.ruby-doc.org/core/classes/OpenURI.html

Kad K. wrote:

In my app, I have an URL to download a file from a remote site , when
using this URL in a browser I download the file…

http://www.anotherdomain.com/resource/download/579633

but I need to to get the content of this file within my app , as I
must encode it and transfer to another app

if I use this url with open-uri, I get the html page, not the file
itself…
should I execute the download within my app ( how) and store it in a
temp file
then open it and encode …

how should I proceed to make it as simple as possible ?

thanks for your feedback

You are on the right track, but if OpenURI is pulling down an HTML page
then you may be pointing at the wrong place. With that sorted all you
need to do is something like the following:

require ‘open-uri’
file = Tempfile.new
file.binmode
open(url, headers) { |data| file.write data.read }

That will give you your content in the file variable, and then you can
encode away.

Kad K. wrote:

If the above link is a URI representing the file … no, it’s a
request to anotherdomain.com for getting the resource with ID 579633

when I use this url in a browser : “http://www.anotherdomain.com/
resource/download/579633”
then the file (a pdf file in this case) is automatically downloaded
I guess the site is a RoR site, and my request is processed and the
file is sent using send_file

so, I cannot use this url has a file url directly… GET “http://
www.anotherdomain.com/resource/download/579633

Unless it is possible to get the exact url that represents the other
resource you’re trying to download I can’t see how you’ll save the
resource locally. Is this a service you have a correspondance with? Look
into how to link directly, maybe contact the site maintainer.

Perhaps somebody else will have a good method for capturing though.

thanks

Yes, I sent them a message asking for an additional API call ( there
is only an upload call…)
I could find the exact url they are calling to get the file for
download… it’s an amazon S3 url, so it has keys + protection
no way to play with it…

I just wanted to add to this… I know I am 5 years too late, but I
didn’t want anyone else to be led astray.

You should be able to pull anything you can download in your browser
into your app server using tempfile and open-uri. I had to play with it
for a while, but
something like this worked:

require “open-uri”
url = “whatev.com/file_id
file_name = “local_filename.pdf”

Tempfile.new(file_name).tap do |file|
file.binmode
file.write(open(url).read)
file.close
end

If the above link is a URI representing the file … no, it’s a
request to anotherdomain.com for getting the resource with ID 579633

when I use this url in a browser : “http://www.anotherdomain.com/
resource/download/579633”
then the file (a pdf file in this case) is automatically downloaded
I guess the site is a RoR site, and my request is processed and the
file is sent using send_file

so, I cannot use this url has a file url directly… GET “http://
www.anotherdomain.com/resource/download/579633