Skip to content

Instantly share code, notes, and snippets.

@meesterdude
Created October 1, 2012 05:07
Show Gist options
  • Save meesterdude/3809556 to your computer and use it in GitHub Desktop.
Save meesterdude/3809556 to your computer and use it in GitHub Desktop.
Automatically fixes missing images by downloading from live site, if present.
# 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