debian-mirror-gitlab/app/models/concerns/avatarable.rb

119 lines
3.9 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Avatarable
extend ActiveSupport::Concern
2018-03-17 18:26:18 +05:30
included do
prepend ShadowMethods
2018-05-09 12:01:36 +05:30
include ObjectStorage::BackgroundMove
2018-11-08 19:23:39 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
2018-11-20 20:47:30 +05:30
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }, if: :avatar_changed?
2018-03-17 18:26:18 +05:30
mount_uploader :avatar, AvatarUploader
2018-11-08 19:23:39 +05:30
after_initialize :add_avatar_to_batch
2018-03-17 18:26:18 +05:30
end
module ShadowMethods
def avatar_url(**args)
# We use avatar_path instead of overriding avatar_url because of carrierwave.
2019-12-04 20:38:33 +05:30
# See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/11001/diffs#note_28659864
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
avatar_path(only_path: args.fetch(:only_path, true), size: args[:size]) || super
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
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-03-17 18:26:18 +05:30
end
def avatar_type
unless self.avatar.image?
2018-05-09 12:01:36 +05:30
errors.add :avatar, "file format is not supported. Please try one of the following supported formats: #{AvatarUploader::IMAGE_EXT.join(', ')}"
2018-03-17 18:26:18 +05:30
end
end
2018-11-18 11:00:15 +05:30
def avatar_path(only_path: true, size: nil)
2019-02-15 15:39:39 +05:30
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-09-10 17:25:29 +05:30
asset_host = ActionController::Base.asset_host
2018-03-17 18:26:18 +05:30
use_asset_host = asset_host.present?
2018-05-09 12:01:36 +05:30
use_authentication = respond_to?(:public?) && !public?
2018-11-18 11:00:15 +05:30
query_params = size&.nonzero? ? "?width=#{size}" : ""
2018-03-17 18:26:18 +05:30
# 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-05-09 12:01:36 +05:30
if use_asset_host && use_authentication
2018-03-17 18:26:18 +05:30
use_asset_host = false
only_path = false
end
2017-09-10 17:25:29 +05:30
2018-11-20 20:47:30 +05:30
url_base = []
2018-03-17 18:26:18 +05:30
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
2017-09-10 17:25:29 +05:30
2018-11-20 20:47:30 +05:30
url_base.join + avatar.local_url + query_params
2017-09-10 17:25:29 +05:30
end
2018-11-08 19:23:39 +05:30
# 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-07-31 22:56:46 +05:30
BatchLoader.for(identifier: identifier, model: self)
.batch(key: self.class, cache: true, replace_methods: false) do |upload_params, loader, args|
2018-11-08 19:23:39 +05:30
model_class = args[:key]
paths = upload_params.flat_map do |params|
params[:model].upload_paths(params[:identifier])
end
2019-02-15 15:39:39 +05:30
Upload.where(uploader: AvatarUploader.name, path: paths).find_each do |upload|
2018-11-08 19:23:39 +05:30
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
2017-09-10 17:25:29 +05:30
end