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
|
|
|
|
2015-11-25 11:42:46 +08:00
|
|
|
skip_before_filter :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter, :show_proxy_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-09-11 18:56:34 +08:00
|
|
|
render json: {
|
|
|
|
gravatar_upload_id: user.user_avatar.gravatar_upload_id,
|
|
|
|
gravatar_avatar_template: User.avatar_template(user.username, user.user_avatar.gravatar_upload_id)
|
|
|
|
}
|
2014-05-26 17:46:43 +08:00
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-25 11:42:46 +08:00
|
|
|
# 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
|
|
|
|
|
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-12-16 11:02:09 +08:00
|
|
|
return render_blank 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
|
2015-05-30 00:51:17 +08:00
|
|
|
show_in_site(params[: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-12-16 10:18:12 +08:00
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
username = params[:username].to_s
|
2015-12-16 11:02:09 +08:00
|
|
|
return render_blank unless user = User.find_by(username_lower: username.downcase)
|
2014-05-22 15:37:02 +08:00
|
|
|
|
2015-05-30 01:19:41 +08:00
|
|
|
upload_id, version = params[:version].split("_")
|
|
|
|
|
|
|
|
version = (version || OptimizedImage::VERSION).to_i
|
2015-12-16 11:02:09 +08:00
|
|
|
return render_blank if version != OptimizedImage::VERSION
|
2015-05-30 01:19:41 +08:00
|
|
|
|
|
|
|
upload_id = upload_id.to_i
|
2015-12-16 11:02:09 +08:00
|
|
|
return render_blank unless upload_id > 0 && user_avatar = user.user_avatar
|
2014-05-22 15:37:02 +08:00
|
|
|
|
2015-05-26 21:54:25 +08:00
|
|
|
size = params[:size].to_i
|
2016-07-06 00:49:33 +08:00
|
|
|
return render_blank if size < 8 || size > 1000
|
2015-05-26 13:41:50 +08:00
|
|
|
|
|
|
|
if !Discourse.avatar_sizes.include?(size) && Discourse.store.external?
|
2015-05-29 15:57:54 +08:00
|
|
|
closest = Discourse.avatar_sizes.to_a.min { |a,b| (size-a).abs <=> (size-b).abs }
|
2015-05-30 01:19:41 +08:00
|
|
|
avatar_url = UserAvatar.local_avatar_url(hostname, user.username_lower, upload_id, closest)
|
2015-05-30 00:51:17 +08:00
|
|
|
return redirect_to cdn_path(avatar_url)
|
2015-05-26 13:41:50 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 01:19:41 +08:00
|
|
|
upload = Upload.find_by(id: upload_id) if user_avatar.contains_upload?(upload_id)
|
|
|
|
upload ||= user.uploaded_avatar if user.uploaded_avatar_id == upload_id
|
2014-05-22 15:37:02 +08:00
|
|
|
|
|
|
|
if user.uploaded_avatar && !upload
|
2015-05-30 00:51:17 +08:00
|
|
|
avatar_url = UserAvatar.local_avatar_url(hostname, user.username_lower, user.uploaded_avatar_id, size)
|
|
|
|
return redirect_to cdn_path(avatar_url)
|
2015-06-01 23:49:58 +08:00
|
|
|
elsif upload && optimized = get_optimized_image(upload, size)
|
|
|
|
if optimized.local?
|
|
|
|
optimized_path = Discourse.store.path_for(optimized)
|
|
|
|
image = optimized_path if File.exists?(optimized_path)
|
|
|
|
else
|
2015-12-16 10:18:12 +08:00
|
|
|
return proxy_avatar(Discourse.store.cdn_url(optimized.url))
|
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
|
2015-12-16 11:02:09 +08:00
|
|
|
render_blank
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
end
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2015-12-16 10:18:12 +08:00
|
|
|
PROXY_PATH = Rails.root + "tmp/avatar_proxy"
|
|
|
|
def proxy_avatar(url)
|
2015-12-17 10:21:09 +08:00
|
|
|
|
|
|
|
if url[0..1] == "//"
|
2016-06-27 17:26:43 +08:00
|
|
|
url = (SiteSetting.force_https ? "https:" : "http:") + url
|
2015-12-17 10:21:09 +08:00
|
|
|
end
|
|
|
|
|
2015-12-16 10:18:12 +08:00
|
|
|
sha = Digest::SHA1.hexdigest(url)
|
|
|
|
filename = "#{sha}#{File.extname(url)}"
|
|
|
|
path = "#{PROXY_PATH}/#{filename}"
|
|
|
|
|
2015-12-16 10:40:34 +08:00
|
|
|
unless File.exist? path
|
2015-12-16 10:18:12 +08:00
|
|
|
FileUtils.mkdir_p PROXY_PATH
|
2016-03-24 20:09:55 +08:00
|
|
|
tmp = FileHelper.download(url, 1.megabyte, filename, true, 10)
|
2015-12-16 10:18:12 +08:00
|
|
|
FileUtils.mv tmp.path, path
|
|
|
|
end
|
|
|
|
|
|
|
|
# putting a bogus date cause download is not retaining the data
|
|
|
|
response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate
|
|
|
|
response.headers["Content-Length"] = File.size(path).to_s
|
|
|
|
expires_in 1.year, public: true
|
|
|
|
send_file path, disposition: nil
|
|
|
|
end
|
|
|
|
|
2014-05-27 20:29:27 +08:00
|
|
|
# this protects us from a DoS
|
2015-12-16 11:02:09 +08:00
|
|
|
def render_blank
|
|
|
|
path = Rails.root + "public/images/avatar.png"
|
2014-05-27 20:29:27 +08:00
|
|
|
expires_in 10.minutes, public: true
|
2015-12-16 11:02:09 +08:00
|
|
|
response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate
|
|
|
|
response.headers["Content-Length"] = File.size(path).to_s
|
|
|
|
send_file path, disposition: nil
|
2014-05-27 20:29:27 +08:00
|
|
|
end
|
|
|
|
|
2014-05-27 12:40:46 +08:00
|
|
|
def get_optimized_image(upload, size)
|
|
|
|
OptimizedImage.create_for(
|
|
|
|
upload,
|
|
|
|
size,
|
|
|
|
size,
|
2015-09-21 04:01:03 +08:00
|
|
|
filename: upload.original_filename,
|
|
|
|
allow_animation: SiteSetting.allow_animated_avatars,
|
2014-05-27 12:40:46 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|