English 中文(简体)
Replacing Broken External Images With Custom Image
原标题:

I m looping through an array of URL strings of images hosted at an external site.

It looks something like this:

def get_image_urls
  image_url_array.each do |image_url|
    puts image_tag image_url
  end
end

Which will return the URLs of images hosted on the external site. The problem is, some of these images might be broken (404). So for example:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://someothersite.com/images/3.jpg" # <-- (Broken: 404)
   "http://someothersite.com/images/4.jpg"
   "http://someothersite.com/images/5.jpg" # <-- (Broken: 404)

What I m looking to do is replace the URL strings of the broken images to a "missing" image hosted on my own site. So using the example above, with 3.jpg and 5.jpg being broken, I want to have returned something like this:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://mysite.com/images/missing.png"
   "http://someothersite.com/images/4.jpg"
   "http://mysite.com/images/missing.png"

Is there a simple way to solve this problem? Thanks so much in advance.

最佳回答

Can t you do a simple request for the image and check if its a 404? Its not perfect but would catch the bulk. Depends how often you are running it. If you don t have access to the files on the server directly to check then a HTTP request is the only way. For speed you could do a header only request. Will need a code example, let me dig you one out...

Depends on what your server will return, if you can get the headers and you just get a standard 404 page then you can check the content-length to ensure its not big enough for an image, that sounds a bit hacky but would work (theres a better way afterwards). Something like:

(take and modified from http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M000682).

response = nil
Net::HTTP.start( www.example.com , 80) {|http|
  response = http.head( /myimage.html )
}
# Assume anything with a content-length greater than 1000 must be an image? 
# You will need to tweek/tune this to your server and your definition of what a good image is
p "Good image" unless response[ content-length ] < 1000

Alternatively you can (and should really to do it the right way) get the HTTP status message as thats the definitive way of the server telling you if the image is there or not. Only trouble is you might have to download the whole thing as I don t know of a quick way of getting just the HTTP status without doing a whole request (see request method in the doc linked above for details).

Hope that helps though.

问题回答

I dont think it is possible to check availability of remote images without periodically requests as Pete described.

But may be you find useful trick I used once (with jquery):

$( img ).error(function(){
 $(this).attr( src ,  <<<REPLACE URL>>> );
});

On error event you can replace host on image url.

Also, you can collect this information by AJAX posts to your host from client and after some amount of such errors - check with Pete method. It will radically decrease amount of needed checkings.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签