2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-10 00:26:17 -04:00
|
|
|
module Avatarable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2020-10-15 23:08:29 -04:00
|
|
|
USER_AVATAR_SIZES = [16, 20, 23, 24, 26, 32, 36, 38, 40, 48, 60, 64, 90, 96, 120, 160].freeze
|
2020-09-28 02:09:56 -04:00
|
|
|
PROJECT_AVATAR_SIZES = [15, 40, 48, 64, 88].freeze
|
|
|
|
GROUP_AVATAR_SIZES = [15, 37, 38, 39, 40, 64, 96].freeze
|
|
|
|
|
|
|
|
ALLOWED_IMAGE_SCALER_WIDTHS = (USER_AVATAR_SIZES | PROJECT_AVATAR_SIZES | GROUP_AVATAR_SIZES).freeze
|
2020-08-10 14:09:54 -04:00
|
|
|
|
2021-06-22 05:07:12 -04:00
|
|
|
# This value must not be bigger than then: https://gitlab.com/gitlab-org/gitlab/-/blob/master/workhorse/config.toml.example#L20
|
|
|
|
#
|
|
|
|
# https://docs.gitlab.com/ee/development/image_scaling.html
|
|
|
|
MAXIMUM_FILE_SIZE = 200.kilobytes.to_i
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
included do
|
|
|
|
prepend ShadowMethods
|
2018-02-21 11:43:21 -05:00
|
|
|
include ObjectStorage::BackgroundMove
|
2018-04-19 12:22:23 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2021-10-13 05:11:55 -04:00
|
|
|
include ApplicationHelper
|
2018-01-29 12:57:34 -05:00
|
|
|
|
|
|
|
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
|
2021-06-22 05:07:12 -04:00
|
|
|
validates :avatar, file_size: { maximum: MAXIMUM_FILE_SIZE }, if: :avatar_changed?
|
2018-01-29 12:57:34 -05:00
|
|
|
|
|
|
|
mount_uploader :avatar, AvatarUploader
|
2018-04-19 12:22:23 -04:00
|
|
|
|
|
|
|
after_initialize :add_avatar_to_batch
|
2021-03-03 01:11:13 -05:00
|
|
|
after_commit :clear_avatar_caches
|
2018-01-29 12:57:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module ShadowMethods
|
|
|
|
def avatar_url(**args)
|
|
|
|
# We use avatar_path instead of overriding avatar_url because of carrierwave.
|
2019-09-18 10:02:45 -04:00
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/11001/diffs#note_28659864
|
2018-01-29 12:57:34 -05:00
|
|
|
|
2018-08-07 02:04:33 -04:00
|
|
|
avatar_path(only_path: args.fetch(:only_path, true), size: args[:size]) || super
|
2018-01-29 12:57:34 -05:00
|
|
|
end
|
2018-04-19 12:22:23 -04:00
|
|
|
|
|
|
|
def retrieve_upload(identifier, paths)
|
|
|
|
upload = retrieve_upload_from_batch(identifier)
|
|
|
|
|
|
|
|
# This fallback is needed when deleting an upload, because we may have
|
|
|
|
# already been removed from the DB. We have to check an explicit `#nil?`
|
|
|
|
# because it's a BatchLoader instance.
|
|
|
|
upload = super if upload.nil?
|
|
|
|
|
|
|
|
upload
|
|
|
|
end
|
2018-01-29 12:57:34 -05:00
|
|
|
end
|
|
|
|
|
2020-07-09 14:10:09 -04:00
|
|
|
class_methods do
|
|
|
|
def bot_avatar(image:)
|
2020-07-23 11:09:28 -04:00
|
|
|
Rails.root.join('lib', 'assets', 'images', 'bot_avatars', image).open
|
2020-07-09 14:10:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def avatar_type
|
|
|
|
unless self.avatar.image?
|
2019-09-23 14:06:14 -04:00
|
|
|
errors.add :avatar, "file format is not supported. Please try one of the following supported formats: #{AvatarUploader::SAFE_IMAGE_EXT.join(', ')}"
|
2018-01-29 12:57:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-07 02:04:33 -04:00
|
|
|
def avatar_path(only_path: true, size: nil)
|
2018-12-19 18:18:20 -05:00
|
|
|
unless self.try(:id)
|
|
|
|
return uncached_avatar_path(only_path: only_path, size: size)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Cache this avatar path only within the request because avatars in
|
|
|
|
# object storage may be generated with time-limited, signed URLs.
|
|
|
|
key = "#{self.class.name}:#{self.id}:#{only_path}:#{size}"
|
|
|
|
Gitlab::SafeRequestStore[key] ||= uncached_avatar_path(only_path: only_path, size: size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def uncached_avatar_path(only_path: true, size: nil)
|
|
|
|
return unless self.try(:avatar).present?
|
2017-05-10 00:26:17 -04:00
|
|
|
|
|
|
|
asset_host = ActionController::Base.asset_host
|
2017-11-09 10:40:41 -05:00
|
|
|
use_asset_host = asset_host.present?
|
2018-04-23 12:59:53 -04:00
|
|
|
use_authentication = respond_to?(:public?) && !public?
|
2018-08-07 02:04:33 -04:00
|
|
|
query_params = size&.nonzero? ? "?width=#{size}" : ""
|
2017-05-10 00:26:17 -04:00
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
# Avatars for private and internal groups and projects require authentication to be viewed,
|
|
|
|
# which means they can only be served by Rails, on the regular GitLab host.
|
|
|
|
# If an asset host is configured, we need to return the fully qualified URL
|
|
|
|
# instead of only the avatar path, so that Rails doesn't prefix it with the asset host.
|
2018-04-23 12:59:53 -04:00
|
|
|
if use_asset_host && use_authentication
|
2017-11-09 10:40:41 -05:00
|
|
|
use_asset_host = false
|
|
|
|
only_path = false
|
|
|
|
end
|
2017-10-02 09:44:58 -04:00
|
|
|
|
2018-08-03 13:22:24 -04:00
|
|
|
url_base = []
|
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
if use_asset_host
|
|
|
|
url_base << asset_host unless only_path
|
|
|
|
else
|
|
|
|
url_base << gitlab_config.base_url unless only_path
|
|
|
|
url_base << gitlab_config.relative_url_root
|
|
|
|
end
|
|
|
|
|
2018-08-03 13:22:24 -04:00
|
|
|
url_base.join + avatar.local_url + query_params
|
2017-05-10 00:26:17 -04:00
|
|
|
end
|
2018-04-19 12:22:23 -04:00
|
|
|
|
|
|
|
# Path that is persisted in the tracking Upload model. Used to fetch the
|
|
|
|
# upload from the model.
|
|
|
|
def upload_paths(identifier)
|
|
|
|
avatar_mounter.blank_uploader.store_dirs.map { |store, path| File.join(path, identifier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def retrieve_upload_from_batch(identifier)
|
2019-04-29 20:07:42 -04:00
|
|
|
BatchLoader.for(identifier: identifier, model: self)
|
2021-09-29 08:11:22 -04:00
|
|
|
.batch(key: self.class) do |upload_params, loader, args|
|
2018-04-19 12:22:23 -04:00
|
|
|
model_class = args[:key]
|
|
|
|
paths = upload_params.flat_map do |params|
|
|
|
|
params[:model].upload_paths(params[:identifier])
|
|
|
|
end
|
|
|
|
|
2018-11-17 10:14:36 -05:00
|
|
|
Upload.where(uploader: AvatarUploader.name, path: paths).find_each do |upload|
|
2018-04-19 12:22:23 -04:00
|
|
|
model = model_class.instantiate('id' => upload.model_id)
|
|
|
|
|
|
|
|
loader.call({ model: model, identifier: File.basename(upload.path) }, upload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_avatar_to_batch
|
|
|
|
return unless avatar_mounter
|
|
|
|
|
|
|
|
avatar_mounter.read_identifiers.each { |identifier| retrieve_upload_from_batch(identifier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_mounter
|
|
|
|
strong_memoize(:avatar_mounter) { _mounter(:avatar) }
|
|
|
|
end
|
2021-03-03 01:11:13 -05:00
|
|
|
|
|
|
|
def clear_avatar_caches
|
|
|
|
return unless respond_to?(:verified_emails) && verified_emails.any? && avatar_changed?
|
|
|
|
|
|
|
|
Gitlab::AvatarCache.delete_by_email(*verified_emails)
|
|
|
|
end
|
2017-05-10 00:26:17 -04:00
|
|
|
end
|