Permalink
Browse files

get default gravator image locally when firewalled

  • Loading branch information...
1 parent f6709bb commit e67fc5b451da1d326cee18302ba19f531c872e37 @avonderluft avonderluft committed Mar 26, 2013
Showing with 24 additions and 10 deletions.
  1. +11 −5 app/helpers/radiant/application_helper.rb
  2. +13 −5 app/models/standard_tags.rb
@@ -188,19 +188,25 @@ def gravatar_url(email, options={})
# Default image url to be used when no gravatar is found
# or when an image exceeds the rating parameter.
- default_avatar_url = "#{request.protocol}#{request.host_with_port}#{ActionController::Base.relative_url_root}/images/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
+ local_avatar_url = "/images/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
+ default_avatar_url = "#{request.protocol}#{request.host_with_port}#{ActionController::Base.relative_url_root}#{local_avatar_url}"
options[:default] ||= default_avatar_url
unless email.blank?
- # Build the Gravatar url.
- url = '//gravatar.com/avatar/'
+ # Build the Gravatar url.
+ url = '//gravatar.com/avatar/'
url << "#{Digest::MD5.new.update(email)}?"
url << "rating=#{options[:rating]}" if options[:rating]
url << "&size=#{options[:size]}" if options[:size]
url << "&default=#{options[:default]}" if options[:default]
- url
+ # Test the Gravatar url
+ require 'open-uri'
+ begin; open "http:#{url}", :proxy => true
+ rescue; local_avatar_url
+ else; url
+ end
else
- default_avatar_url
+ local_avatar_url
end
end
@@ -730,17 +730,25 @@ class RequiredAttributeError < StandardError; end
name = (tag.attr['name'] || page.created_by.name)
rating = (tag.attr['rating'] || 'G')
size = (tag.attr['size'] || '32px')
- email = User.find_by_name(name).email
- default = "#{request.protocol}#{request.host_with_port}/images/admin/avatar_#{([size.to_i] * 2).join('x')}.png"
+ user = User.find_by_name(name)
+ email = user ? user.email : nil
+ local_avatar_url = "/images/admin/avatar_#{([size.to_i] * 2).join('x')}.png"
+ default_avatar_url = "#{request.protocol}#{request.host_with_port}#{local_avatar_url}"
+
unless email.blank?
url = '//gravatar.com/avatar/'
url << "#{Digest::MD5.new.update(email)}?"
url << "rating=#{rating}"
url << "&size=#{size.to_i}"
- url << "&default=#{default}"
- url
+ url << "&default=#{default_avatar_url}"
+ # Test the Gravatar url
+ require 'open-uri'
+ begin; open "http:#{url}", :proxy => true
+ rescue; local_avatar_url
+ else; url
+ end
else
- default
+ local_avatar_url
end
end

0 comments on commit e67fc5b

Please sign in to comment.