Displaying images from this controllers code


#1

Hi,

Im pretty new to rails so please excuse me if this is a trivial question
to most of you :slight_smile:

Im making use of the following code that I came accross which allows me
to go and feed amazons alexa service a url and it returns a thumbnaill.

class AmazonController < ApplicationController

layout ‘default’
def index
require ‘cgi’
require ‘openssl’
require ‘base64’
require ‘open-uri’

access_id = 'XXXXX'
secret_id = 'XXXXXXXXXX'

source_url = 'google.com'

timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
sig =

Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new(‘SHA1’),
secret_id, ‘Thumbnail’ + timestamp)).strip

url =

http://ast.amazonaws.com/Xino?Action=Thumbnail&AWSAccessKeyId=” +
access_id
url << “&Signature=” + CGI.escape(sig)
url << “&Timestamp=” + CGI.escape(timestamp)
url << “&Url=” + source_url

begin
  doc = open(url).read
rescue
  puts "Could not access AWS"
  exit
end

m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)

if m && m[1]
  thumb_url = m[1]
  thumb_url.gsub!(/\&amp;/, '&')
  File.open("#{source_url}.jpg", "w") { |f| f.write

open(thumb_url).read }
puts “Saved to #{source_url}.jpg”
elsif m && m.match(/exists=“false”/)
puts “No thumbnail for #{source_url}”
else
puts “Error”
end

end
end

This all works fine when i fire it off as a ruby script and places the
thumbnail in the same directory as the ruby script. what I want now
though is to bring the same code to rails. I have placed it in a
controller as you can see above and I would like now to display these
thumbnails on the front end view.

How does one access this information and bring it up on the front end? I
dont entirely understand how the image is written to disk which could be
part of the problem.

Cheers