Skip to content

Instantly share code, notes, and snippets.

@maxlapshin
Created March 20, 2009 15:53
Show Gist options
  • Save maxlapshin/82411 to your computer and use it in GitHub Desktop.
Save maxlapshin/82411 to your computer and use it in GitHub Desktop.
Production Images Download
# В config/initializers/development.rb
#
# config.middleware.insert_before(::Rack::Lock, ::ProductionImagesDownload, "www.lookatme.ru")
class ProductionImagesDownload
# Перехватывает отсутствующие картинки и пытается слить их с lookatme.ru
def initialize(app, host)
Rails.logger.debug "Production images downloader started"
@app = app
@host = host
@path = (Rails.root+"/public").to_s
@static = Rack::File.new(@path)
end
def call(env)
req_path = env["PATH_INFO"]
return @app.call(env) unless (req_path.index("/assets") == 0 || req_path.index("/images/hairvideo") == 0)
if !File.exists?(@path + req_path)
FileUtils.mkdir_p(File.dirname(@path + req_path))
Rails.logger.debug "Downloading: #{req_path.inspect}"
`curl 'http://#{@host}#{req_path}' -o '#{@path + req_path}' >/dev/null 2>/dev/null`
Rails.logger.debug "Downloaded: #{req_path.inspect}"
end
@static.call(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment