FIX: show letter avatars even if NGINX is not running in Dev mode

This commit is contained in:
Sam 2015-11-25 14:42:46 +11:00
parent 0844350308
commit d41a8a21cc
3 changed files with 38 additions and 15 deletions

View File

@ -3,7 +3,7 @@ require_dependency 'letter_avatar'
class UserAvatarsController < ApplicationController class UserAvatarsController < ApplicationController
DOT = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") DOT = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
skip_before_filter :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter] skip_before_filter :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter, :show_proxy_letter]
def refresh_gravatar def refresh_gravatar
user = User.find_by(username_lower: params[:username].downcase) user = User.find_by(username_lower: params[:username].downcase)
@ -22,6 +22,26 @@ class UserAvatarsController < ApplicationController
end end
end end
# mainly used in development for backwards compat
def show_proxy_letter
params.require(:letter)
params.require(:color)
params.require(:version)
params.require(:size)
no_cookies
identity = LetterAvatar::Identity.new
identity.letter = params[:letter].to_s[0].upcase
identity.color = params[:color].scan(/../).map(&:hex)
image = LetterAvatar.generate(params[:letter].to_s, params[:size].to_i, identity: identity)
response.headers["Last-Modified"] = File.ctime(image).httpdate
response.headers["Content-Length"] = File.size(image).to_s
expires_in 1.year, public: true
send_file image, disposition: nil
end
def show_letter def show_letter
params.require(:username) params.require(:username)
params.require(:version) params.require(:version)

View File

@ -314,6 +314,9 @@ Discourse::Application.routes.draw do
get "letter_avatar/:username/:size/:version.png" => "user_avatars#show_letter", format: false, constraints: { hostname: /[\w\.-]+/, size: /\d+/, username: USERNAME_ROUTE_FORMAT} get "letter_avatar/:username/:size/:version.png" => "user_avatars#show_letter", format: false, constraints: { hostname: /[\w\.-]+/, size: /\d+/, username: USERNAME_ROUTE_FORMAT}
get "user_avatar/:hostname/:username/:size/:version.png" => "user_avatars#show", format: false, constraints: { hostname: /[\w\.-]+/, size: /\d+/, username: USERNAME_ROUTE_FORMAT } get "user_avatar/:hostname/:username/:size/:version.png" => "user_avatars#show", format: false, constraints: { hostname: /[\w\.-]+/, size: /\d+/, username: USERNAME_ROUTE_FORMAT }
# in most production settings this is bypassed
get "letter_avatar_proxy/:version/letter/:letter/:color/:size.png" => "user_avatars#show_proxy_letter"
get "highlight-js/:hostname/:version.js" => "highlight_js#show", format: false, constraints: { hostname: /[\w\.-]+/ } get "highlight-js/:hostname/:version.js" => "highlight_js#show", format: false, constraints: { hostname: /[\w\.-]+/ }
get "stylesheets/:name.css" => "stylesheets#show", constraints: { name: /[a-z0-9_]+/ } get "stylesheets/:name.css" => "stylesheets#show", constraints: { name: /[a-z0-9_]+/ }

View File

@ -1,5 +1,18 @@
class LetterAvatar class LetterAvatar
class Identity
attr_accessor :color, :letter
def self.from_username(username)
identity = new
identity.color = LetterAvatar::COLORS[
Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length
]
identity.letter = username[0].upcase
identity
end
end
# BUMP UP if avatar algorithm changes # BUMP UP if avatar algorithm changes
VERSION = 5 VERSION = 5
@ -9,19 +22,6 @@ class LetterAvatar
class << self class << self
class Identity
attr_accessor :color, :letter
def self.from_username(username)
identity = new
identity.color = LetterAvatar::COLORS[
Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length
]
identity.letter = username[0].upcase
identity
end
end
def version def version
"#{VERSION}_#{image_magick_version}" "#{VERSION}_#{image_magick_version}"
end end
@ -32,7 +32,7 @@ class LetterAvatar
def generate(username, size, opts = nil) def generate(username, size, opts = nil)
DistributedMutex.synchronize("letter_avatar_#{version}_#{username}") do DistributedMutex.synchronize("letter_avatar_#{version}_#{username}") do
identity = Identity.from_username(username) identity = (opts && opts[:identity]) || LetterAvatar::Identity.from_username(username)
cache = true cache = true
cache = false if opts && opts[:cache] == false cache = false if opts && opts[:cache] == false