Created
March 20, 2009 15:53
-
-
Save maxlapshin/82411 to your computer and use it in GitHub Desktop.
Production Images Download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# В 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