Rails: open() returns StringIO instead of Tempfile -


i have 2 valid url's 2 images. when run open() on first url, returns object of type tempfile (which fog gem expects upload image aws). when run open() on second url, returns object of type stringio (which causes fog gem crash , burn).

why open() not returning tempfile second url? further, can open() forced return tempfile?

from rails console:

2.2.1 :011 > url1  => "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/c0.0.448.448/10298878_10103685138839040_6456490261359194847_n.jpg?oh=e2951e1a1b0a04fc2b9c0a0b0b191ebc&oe=56195ee3&__gda__=1443959086_417127efe9c89652ec44058c360ee6de"  2.2.1 :012 > url2  => "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/c0.17.200.200/1920047_10153890268465074_1858953512_n.jpg?oh=5f4cdf53d3e59b8ce4702618b3ac6ce3&oe=5610adc5&__gda__=1444367255_396d6fdc0bdc158e4c2e3127e86878f9"  2.2.1 :013 > t1 = open(url1)  => #<tempfile:/var/folders/58/lpjz5b0n3yj44vn9bmbrv5180000gn/t/open-uri20150720-24696-1y0kvtd>  2.2.1 :014 > t2 = open(url2)  => #<stringio:0x007fba9c20ae78 @base_uri=#<uri::https https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/c0.17.200.200/1920047_10153890268465074_1858953512_n.jpg?oh=5f4cdf53d3e59b8ce4702618b3ac6ce3&oe=5610adc5&__gda__=1444367255_396d6fdc0bdc158e4c2e3127e86878f9>, @meta={"last-modified"=>"tue, 25 feb 2014 19:47:06 gmt", "content-type"=>"image/jpeg", "timing-allow-origin"=>"*", "access-control-allow-origin"=>"*", "content-length"=>"7564", "cache-control"=>"no-transform, max-age=1209600", "expires"=>"mon, 03 aug 2015 22:01:40 gmt", "date"=>"mon, 20 jul 2015 22:01:40 gmt", "connection"=>"keep-alive"}, @metas={"last-modified"=>["tue, 25 feb 2014 19:47:06 gmt"], "content-type"=>["image/jpeg"], "timing-allow-origin"=>["*"], "access-control-allow-origin"=>["*"], "content-length"=>["7564"], "cache-control"=>["no-transform, max-age=1209600"], "expires"=>["mon, 03 aug 2015 22:01:40 gmt"], "date"=>["mon, 20 jul 2015 22:01:40 gmt"], "connection"=>["keep-alive"]}, @status=["200", "ok"]> 

this how i'm using fog:

tempfile = open(params["avatar"]) user.avatar.store!(tempfile) 

i assume using ruby's built-in open-uri library allows download urls using open().

in case ruby obligated return io object. there no guarantee file. guess ruby makes decision based on memory consumption: if download large, puts file save memory; otherwise keeps in memory stringio.

as workaround, write method writes stream tempfile if not downloaded file:

def download_to_file(uri)   stream = open(uri, "rb")   return stream if stream.respond_to?(:path) # file-like    tempfile.new.tap |file|     file.binmode     io.copy_stream(stream, file)     stream.close     file.rewind   end end 

if you're looking full-featured gem similar, take @ "down": https://github.com/janko-m/down


Comments