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

56 lines
1.8 KiB
Ruby
Raw Normal View History

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-03-17 18:26:18 +05:30
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
mount_uploader :avatar, AvatarUploader
end
module ShadowMethods
def avatar_url(**args)
# We use avatar_path instead of overriding avatar_url because of carrierwave.
# See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
avatar_path(only_path: args.fetch(:only_path, true)) || super
end
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
2017-09-10 17:25:29 +05:30
def avatar_path(only_path: true)
return unless self[:avatar].present?
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-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-03-17 18:26:18 +05:30
url_base = ""
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-05-09 12:01:36 +05:30
url_base + avatar.local_url
2017-09-10 17:25:29 +05:30
end
end