Rails image proxy ActionController::RoutingError
After updateing to Rails 3.2.13 from Rails 3.0.9 my rack middelware image
proxy is broken.
On localhost the image is served but I still get a routing error:
Started GET "/assets/vind/original/64/vind_gavekort_valgfrit.png?1231231"
for 12
7.0.0.1 at 2013-08-07 09:36:04 +0200
Served asset /vind/original/64/vind_gavekort_valgfrit.png - 404 Not Found
(9ms)
ActionController::RoutingError (No route matches [GET]
"/assets/vind/original/64
/vind_gavekort_valgfrit.png"):
lib/redirect_it.rb:19:in `call'
Rendered
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_disp
atch/middleware/templates/rescues/routing_error.erb within rescues/layout
(1.0ms
)
In application.rb:
config.threadsafe!
config.middleware.insert_before 0, "RedirectIt"
In lib folder:
class RedirectIt
require "net/https"
require "uri"
require 'open-uri'
APP_DOMAIN = 'www.mydomain.com'
def initialize(app)
@app = app
end
#
def call(env)
request = Rack::Request.new(env)
#if request.env['HTTP_HOST'] != APP_DOMAIN
#REDIRECT TIL WWW
#[301, { "Location" => "http://#{APP_DOMAIN}#{request.fullpath()}"},
["Redirecting..."]]
if "#{request.path()}".split("/")[2] == "vind" and
"#{request.path()}".split(".").last[0..2] == "png"
#PROXY BILLEDER
status, headers, response = @app.call(env)
headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
headers['Content-Type'] = 'image/png'
headers['Content-Disposition'] = 'inline'
url = "http://s3.amazonaws.com/someimage.png"
response_body = "#{(open(url)).read}"
headers["Content-Length"] = response_body.length.to_s
[status, headers, [response_body]]
else
@app.call(env)
end
end
end
No comments:
Post a Comment