mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
FIX: allow handling for avatars that are not in the set of "resized sizes"
This commit is contained in:
parent
e17f614771
commit
147ea002f7
5 changed files with 58 additions and 9 deletions
|
@ -50,7 +50,7 @@ class UserAvatarsController < ApplicationController
|
|||
|
||||
def show_in_site(hostname)
|
||||
size = params[:size].to_i
|
||||
return render_dot unless Discourse.avatar_sizes.include?(size)
|
||||
|
||||
|
||||
username = params[:username].to_s
|
||||
return render_dot unless user = User.find_by(username_lower: username.downcase)
|
||||
|
@ -58,11 +58,21 @@ class UserAvatarsController < ApplicationController
|
|||
version = params[:version].to_i
|
||||
return render_dot unless version > 0 && user_avatar = user.user_avatar
|
||||
|
||||
# some sanity checks
|
||||
if size < 8 || size > 500
|
||||
return render_dot
|
||||
end
|
||||
|
||||
if !Discourse.avatar_sizes.include?(size) && Discourse.store.external?
|
||||
closest = Discourse.avatar_sizes.to_a.min{|a,b| (size-a).abs <=> (size-b).abs}
|
||||
return redirect_to cdn_path("/user_avatar/#{params[:hostname]}/#{user.username_lower}/#{closest}/#{version}.png")
|
||||
end
|
||||
|
||||
upload = Upload.find_by(id: version) if user_avatar.contains_upload?(version)
|
||||
upload ||= user.uploaded_avatar if user.uploaded_avatar_id == version
|
||||
|
||||
if user.uploaded_avatar && !upload
|
||||
return redirect_to path("/user_avatar/#{hostname}/#{user.username_lower}/#{size}/#{user.uploaded_avatar_id}.png")
|
||||
return redirect_to cdn_path("/user_avatar/#{hostname}/#{user.username_lower}/#{size}/#{user.uploaded_avatar_id}.png")
|
||||
elsif upload
|
||||
original = Discourse.store.path_for(upload)
|
||||
if Discourse.store.external? || File.exists?(original)
|
||||
|
|
|
@ -79,7 +79,13 @@ module Discourse
|
|||
end
|
||||
|
||||
def self.avatar_sizes
|
||||
@avatar_size ||= Set.new(SiteSetting.avatar_sizes.split("|").map(&:to_i))
|
||||
# Don't cache until we can get a notification from site settings to expire cache
|
||||
set = Set.new(SiteSetting.avatar_sizes.split("|").map(&:to_i))
|
||||
# add retinas which are 2x dpi
|
||||
set.to_a.each do |size|
|
||||
set << size*2
|
||||
end
|
||||
set
|
||||
end
|
||||
|
||||
def self.activate_plugins!
|
||||
|
|
|
@ -2,4 +2,8 @@ module GlobalPath
|
|||
def path(p)
|
||||
"#{GlobalSetting.relative_url_root}#{p}"
|
||||
end
|
||||
|
||||
def cdn_path(p)
|
||||
"#{GlobalSetting.cdn_url}#{path(p)}"
|
||||
end
|
||||
end
|
||||
|
|
35
spec/controllers/user_avatars_controller_spec.rb
Normal file
35
spec/controllers/user_avatars_controller_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe UserAvatarsController do
|
||||
|
||||
context 'show' do
|
||||
it 'redirects to correct size for non local' do
|
||||
SiteSetting.avatar_sizes = "100|49"
|
||||
SiteSetting.enable_s3_uploads = true
|
||||
SiteSetting.s3_access_key_id = "XXX"
|
||||
SiteSetting.s3_secret_access_key = "XXX"
|
||||
SiteSetting.s3_upload_bucket = "test"
|
||||
|
||||
GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom")
|
||||
|
||||
|
||||
upload = Fabricate(:upload)
|
||||
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
||||
|
||||
get :show, size: 97, username: user.username, version: upload.id, hostname: 'default'
|
||||
|
||||
# 98 is closese which is 49 * 2 for retina
|
||||
expect(response).to redirect_to("http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}.png")
|
||||
end
|
||||
|
||||
it 'serves image even if size missing and its in local mode' do
|
||||
SiteSetting.avatar_sizes = "50"
|
||||
|
||||
upload = Fabricate(:upload)
|
||||
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
||||
|
||||
get :show, size: 51, username: user.username, version: upload.id, hostname: 'default'
|
||||
expect(response).to be_success
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,12 +2,6 @@ require 'spec_helper'
|
|||
require 'digest/sha1'
|
||||
|
||||
describe Upload do
|
||||
it { is_expected.to belong_to :user }
|
||||
|
||||
it { is_expected.to have_many :post_uploads }
|
||||
it { is_expected.to have_many :posts }
|
||||
|
||||
it { is_expected.to have_many :optimized_images }
|
||||
|
||||
let(:upload) { build(:upload) }
|
||||
let(:thumbnail) { build(:optimized_image, upload: upload) }
|
||||
|
|
Loading…
Reference in a new issue