From d41a8a21cc76fb50c0121942e7c04a1e8c34ffaa Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 25 Nov 2015 14:42:46 +1100 Subject: [PATCH] FIX: show letter avatars even if NGINX is not running in Dev mode --- app/controllers/user_avatars_controller.rb | 22 ++++++++++++++++- config/routes.rb | 3 +++ lib/letter_avatar.rb | 28 +++++++++++----------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index 1ff19e3ccfc..7468fd905e4 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -3,7 +3,7 @@ require_dependency 'letter_avatar' class UserAvatarsController < ApplicationController 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 user = User.find_by(username_lower: params[:username].downcase) @@ -22,6 +22,26 @@ class UserAvatarsController < ApplicationController 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 params.require(:username) params.require(:version) diff --git a/config/routes.rb b/config/routes.rb index 2fc80e4eb29..57486b0dcd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 "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 "stylesheets/:name.css" => "stylesheets#show", constraints: { name: /[a-z0-9_]+/ } diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb index abde76ec080..7c79c033e9d 100644 --- a/lib/letter_avatar.rb +++ b/lib/letter_avatar.rb @@ -1,5 +1,18 @@ 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 VERSION = 5 @@ -9,19 +22,6 @@ class LetterAvatar 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 "#{VERSION}_#{image_magick_version}" end @@ -32,7 +32,7 @@ class LetterAvatar def generate(username, size, opts = nil) DistributedMutex.synchronize("letter_avatar_#{version}_#{username}") do - identity = Identity.from_username(username) + identity = (opts && opts[:identity]) || LetterAvatar::Identity.from_username(username) cache = true cache = false if opts && opts[:cache] == false