ruby on rails - Rendering an image -


so should pretty easy, yet can't work.

i have controller method finds image based on query, output gets cached. image remote (flickr, google images, etc) or local. regardless of source, need take image file contents, , pass through user. in essence, proxy. passing through remote images seems work fine, passing through local images gives me a:

invalid byte sequence in utf-8 

so here's got. i'm hoping can solve problem or guide me in better direction code.

def image_proxy   query = params[:query]   image_url = get_image_url(query) # returns absolute local file path or url    response.headers['cache-control'] = "public, max-age=#{12.hours.to_i}"   response.headers['content-type'] = 'image/jpeg'   response.headers['content-disposition'] = 'inline'   render :text => open(image_url).read end 

remote files work fine, local files don't.

bonus can solve other issue:

  1. i need set proper content type. remote image urls don't tell me image type, url , url doesn't contain extension. picked jpeg because seems work regardless of image type sent me.

thanks!

try using render :text => open(image_url, "rb").read, tells ruby file opens binary, , not try reading text.

edit

for bonus question, read first few bytes , @ contain. png start hexadecimal byte values 89 50 4e 47 0d 0a 1a 0a (or decimal values 137 80 78 71 13 10 26 10).

wikipedia has list of magic numbers used identify file can at. create method of sort reads first few bytes , compares that.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -