2014-05-22 15:37:02 +08:00
|
|
|
require_dependency 'letter_avatar'
|
2017-05-11 06:16:57 +08:00
|
|
|
require_dependency 'upload_creator'
|
2014-05-22 15:37:02 +08:00
|
|
|
|
|
|
|
class UserAvatar < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :gravatar_upload, class_name: 'Upload', dependent: :destroy
|
|
|
|
belongs_to :custom_upload, class_name: 'Upload', dependent: :destroy
|
|
|
|
|
|
|
|
def contains_upload?(id)
|
2014-05-30 12:17:35 +08:00
|
|
|
gravatar_upload_id == id || custom_upload_id == id
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_gravatar!
|
2015-09-18 01:42:44 +08:00
|
|
|
DistributedMutex.synchronize("update_gravatar_#{user_id}") do
|
2015-05-07 07:00:13 +08:00
|
|
|
begin
|
|
|
|
# special logic for our system user
|
2015-09-18 01:42:44 +08:00
|
|
|
email_hash = user_id == Discourse::SYSTEM_USER_ID ? User.email_hash("info@discourse.org") : user.email_hash
|
2015-05-07 07:00:13 +08:00
|
|
|
|
|
|
|
self.last_gravatar_download_attempt = Time.new
|
|
|
|
|
2015-05-30 00:09:47 +08:00
|
|
|
max = Discourse.avatar_sizes.max
|
|
|
|
gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404"
|
2017-05-25 01:42:52 +08:00
|
|
|
tempfile = FileHelper.download(
|
|
|
|
gravatar_url,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
2017-05-25 01:46:57 +08:00
|
|
|
tmp_file_name: "gravatar",
|
|
|
|
skip_rate_limit: true
|
2017-05-25 01:42:52 +08:00
|
|
|
)
|
2017-05-25 01:29:50 +08:00
|
|
|
if tempfile
|
|
|
|
upload = UploadCreator.new(tempfile, 'gravatar.png', origin: gravatar_url, type: "avatar").create_for(user_id)
|
|
|
|
|
|
|
|
if gravatar_upload_id != upload.id
|
|
|
|
gravatar_upload.try(:destroy!) rescue nil
|
|
|
|
self.gravatar_upload = upload
|
|
|
|
save!
|
|
|
|
end
|
2015-05-07 07:00:13 +08:00
|
|
|
end
|
|
|
|
rescue OpenURI::HTTPError
|
|
|
|
save!
|
|
|
|
rescue SocketError
|
|
|
|
# skip saving, we are not connected to the net
|
|
|
|
ensure
|
|
|
|
tempfile.try(:close!)
|
|
|
|
end
|
2014-05-26 17:46:43 +08:00
|
|
|
end
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:51:17 +08:00
|
|
|
def self.local_avatar_url(hostname, username, upload_id, size)
|
2015-09-11 21:18:17 +08:00
|
|
|
self.local_avatar_template(hostname, username, upload_id).gsub("{size}", size.to_s)
|
2015-05-30 00:51:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_avatar_template(hostname, username, upload_id)
|
|
|
|
version = self.version(upload_id)
|
|
|
|
"#{Discourse.base_uri}/user_avatar/#{hostname}/#{username}/{size}/#{version}.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.external_avatar_url(user_id, upload_id, size)
|
2015-09-11 21:18:17 +08:00
|
|
|
self.external_avatar_template(user_id, upload_id).gsub("{size}", size.to_s)
|
2015-05-30 00:51:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.external_avatar_template(user_id, upload_id)
|
|
|
|
version = self.version(upload_id)
|
|
|
|
"#{Discourse.store.absolute_base_url}/avatars/#{user_id}/{size}/#{version}.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(upload_id)
|
|
|
|
"#{upload_id}_#{OptimizedImage::VERSION}"
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.import_url_for_user(avatar_url, user, options = nil)
|
2017-05-25 01:42:52 +08:00
|
|
|
tempfile = FileHelper.download(
|
|
|
|
avatar_url,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
|
|
|
tmp_file_name: "sso-avatar",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2015-06-24 03:59:50 +08:00
|
|
|
|
2017-05-26 11:33:12 +08:00
|
|
|
return unless tempfile
|
|
|
|
|
2015-06-24 03:59:50 +08:00
|
|
|
ext = FastImage.type(tempfile).to_s
|
|
|
|
tempfile.rewind
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
upload = UploadCreator.new(tempfile, "external-avatar." + ext, origin: avatar_url, type: "avatar").create_for(user.id)
|
2015-06-24 03:59:50 +08:00
|
|
|
|
2016-10-17 21:43:40 +08:00
|
|
|
user.create_user_avatar unless user.user_avatar
|
2015-06-24 03:59:50 +08:00
|
|
|
|
|
|
|
if !user.user_avatar.contains_upload?(upload.id)
|
2016-09-19 13:10:02 +08:00
|
|
|
user.user_avatar.update_columns(custom_upload_id: upload.id)
|
|
|
|
|
|
|
|
override_gravatar = !options || options[:override_gravatar]
|
|
|
|
|
|
|
|
if user.uploaded_avatar_id.nil? ||
|
|
|
|
!user.user_avatar.contains_upload?(user.uploaded_avatar_id) ||
|
|
|
|
override_gravatar
|
|
|
|
user.update_columns(uploaded_avatar_id: upload.id)
|
|
|
|
end
|
2015-06-24 03:59:50 +08:00
|
|
|
end
|
2016-09-19 13:10:02 +08:00
|
|
|
|
2017-05-18 00:38:18 +08:00
|
|
|
rescue Net::ReadTimeout, OpenURI::HTTPError
|
2015-06-24 03:59:50 +08:00
|
|
|
# skip saving, we are not connected to the net
|
|
|
|
ensure
|
|
|
|
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_avatars
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# custom_upload_id :integer
|
|
|
|
# gravatar_upload_id :integer
|
|
|
|
# last_gravatar_download_attempt :datetime
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-05-28 09:50:49 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2016-11-24 10:13:03 +08:00
|
|
|
# index_user_avatars_on_custom_upload_id (custom_upload_id)
|
|
|
|
# index_user_avatars_on_gravatar_upload_id (gravatar_upload_id)
|
|
|
|
# index_user_avatars_on_user_id (user_id)
|
2014-05-22 15:37:02 +08:00
|
|
|
#
|