Created
October 1, 2012 05:07
-
-
Save meesterdude/3809556 to your computer and use it in GitHub Desktop.
Automatically fixes missing images by downloading from live site, if present.
This file contains 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
# check pages for missing images. if present on live site, pull down to same path. | |
# in app controller | |
def correct_missing_file | |
return unless Rails.env == 'development' | |
tpath = params[:file].split('?').first | |
fpath = tpath.split('/') | |
fpath.pop | |
fpath = fpath.join('/') | |
uri = URI('http://www.creativebug.com' + tpath) | |
res = Net::HTTP.get_response(uri) | |
if res.code == "200" | |
FileUtils.mkdir_p 'public' + fpath unless File.exists?('public' + fpath) | |
file = File.new('public' + tpath, "wb" ) | |
file.write res.body | |
file.close | |
p "all went well" | |
else | |
p "an error correcting has occured for file #{params[:file]}" | |
end | |
render :nothing => true | |
end | |
# js to run | |
$(document).ready(function() { | |
$("img").error(function(){ | |
console.log("could not find the image: " + $(this).attr('src')) | |
$.post("http://127.0.0.1:3000/missingfile", { file: $(this).attr('src')} ); | |
}); | |
}); | |
# in routes | |
match "/missingfile" => "application#correct_missing_file", :as => "missingfile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment