2014-05-22 15:37:02 +08:00
|
|
|
require_dependency 'letter_avatar'
|
|
|
|
|
2014-05-26 17:46:43 +08:00
|
|
|
class UserAvatarsController < ApplicationController
|
2014-05-27 20:29:27 +08:00
|
|
|
DOT = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
|
|
|
|
|
2015-05-20 15:12:16 +08:00
|
|
|
skip_before_filter :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter]
|
2014-05-26 17:46:43 +08:00
|
|
|
|
|
|
|
def refresh_gravatar
|
|
|
|
user = User.find_by(username_lower: params[:username].downcase)
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
if user
|
|
|
|
user.create_user_avatar(user_id: user.id) unless user.user_avatar
|
|
|
|
user.user_avatar.update_gravatar!
|
|
|
|
|
2015-02-04 01:44:18 +08:00
|
|
|
render json: { upload_id: user.user_avatar.gravatar_upload_id }
|
2014-05-26 17:46:43 +08:00
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-30 12:17:35 +08:00
|
|
|
def show_letter
|
|
|
|
params.require(:username)
|
|
|
|
params.require(:version)
|
|
|
|
params.require(:size)
|
|
|
|
|
2015-05-22 14:15:46 +08:00
|
|
|
no_cookies
|
|
|
|
|
2015-04-20 04:41:08 +08:00
|
|
|
return render_dot if params[:version] != LetterAvatar.version
|
2014-05-30 12:17:35 +08:00
|
|
|
|
|
|
|
image = LetterAvatar.generate(params[:username].to_s, params[:size].to_i)
|
2014-10-22 21:39:51 +08:00
|
|
|
|
2014-07-08 15:20:27 +08:00
|
|
|
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
2014-10-22 21:39:51 +08:00
|
|
|
response.headers["Content-Length"] = File.size(image).to_s
|
2014-05-30 12:17:35 +08:00
|
|
|
expires_in 1.year, public: true
|
|
|
|
send_file image, disposition: nil
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
def show
|
2015-05-22 14:15:46 +08:00
|
|
|
|
|
|
|
no_cookies
|
|
|
|
|
2014-05-27 21:13:42 +08:00
|
|
|
# we need multisite support to keep a single origin pull for CDNs
|
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
2014-05-27 22:15:09 +08:00
|
|
|
show_in_site(RailsMultisite::ConnectionManagement.current_hostname)
|
2014-05-27 21:13:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-05-27 22:15:09 +08:00
|
|
|
def show_in_site(hostname)
|
2015-05-25 23:59:00 +08:00
|
|
|
size = params[:size].to_i
|
|
|
|
return render_dot unless Discourse.avatar_sizes.include?(size)
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
username = params[:username].to_s
|
2014-05-27 20:29:27 +08:00
|
|
|
return render_dot unless user = User.find_by(username_lower: username.downcase)
|
2014-05-22 15:37:02 +08:00
|
|
|
|
|
|
|
version = params[:version].to_i
|
2014-05-27 20:29:27 +08:00
|
|
|
return render_dot unless version > 0 && user_avatar = user.user_avatar
|
2014-05-22 15:37:02 +08:00
|
|
|
|
2014-07-25 13:26:43 +08:00
|
|
|
upload = Upload.find_by(id: version) if user_avatar.contains_upload?(version)
|
2014-05-22 15:37:02 +08:00
|
|
|
upload ||= user.uploaded_avatar if user.uploaded_avatar_id == version
|
|
|
|
|
|
|
|
if user.uploaded_avatar && !upload
|
2015-03-09 08:45:36 +08:00
|
|
|
return redirect_to path("/user_avatar/#{hostname}/#{user.username_lower}/#{size}/#{user.uploaded_avatar_id}.png")
|
2014-05-22 15:37:02 +08:00
|
|
|
elsif upload
|
2014-05-26 17:46:43 +08:00
|
|
|
original = Discourse.store.path_for(upload)
|
2014-05-27 12:40:46 +08:00
|
|
|
if Discourse.store.external? || File.exists?(original)
|
2015-05-25 23:59:00 +08:00
|
|
|
if optimized = get_optimized_image(upload, size)
|
2015-05-26 10:32:52 +08:00
|
|
|
unless optimized.local?
|
2014-09-25 04:52:09 +08:00
|
|
|
expires_in 1.day, public: true
|
|
|
|
return redirect_to optimized.url
|
|
|
|
end
|
|
|
|
image = Discourse.store.path_for(optimized)
|
|
|
|
end
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if image
|
2014-07-08 15:16:07 +08:00
|
|
|
response.headers["Last-Modified"] = File.ctime(image).httpdate
|
2014-10-22 21:39:51 +08:00
|
|
|
response.headers["Content-Length"] = File.size(image).to_s
|
2014-05-22 15:37:02 +08:00
|
|
|
expires_in 1.year, public: true
|
|
|
|
send_file image, disposition: nil
|
|
|
|
else
|
2014-05-27 20:29:27 +08:00
|
|
|
render_dot
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
end
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2014-05-27 20:29:27 +08:00
|
|
|
# this protects us from a DoS
|
|
|
|
def render_dot
|
|
|
|
expires_in 10.minutes, public: true
|
|
|
|
render text: DOT, content_type: "image/png"
|
|
|
|
end
|
|
|
|
|
2014-05-27 12:40:46 +08:00
|
|
|
def get_optimized_image(upload, size)
|
|
|
|
OptimizedImage.create_for(
|
|
|
|
upload,
|
|
|
|
size,
|
|
|
|
size,
|
|
|
|
allow_animation: SiteSetting.allow_animated_avatars
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|