Module: Avatarable

Extended by:
ActiveSupport::Concern
Included in:
Achievements::Achievement, Group, Project, Projects::Topic, User
Defined in:
app/models/concerns/avatarable.rb

Defined Under Namespace

Modules: ShadowMethods

Constant Summary collapse

USER_AVATAR_SIZES =
[16, 20, 23, 24, 26, 32, 36, 38, 40, 48, 60, 64, 90, 96, 120, 160].freeze
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
MAXIMUM_FILE_SIZE =
200.kilobytes.to_i

Instance Method Summary collapse

Instance Method Details

#avatar_path(only_path: true, size: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'app/models/concerns/avatarable.rb', line 57

def avatar_path(only_path: true, size: nil)
  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

#avatar_typeObject



51
52
53
54
55
# File 'app/models/concerns/avatarable.rb', line 51

def avatar_type
  unless self.avatar.image?
    errors.add :avatar, "file format is not supported. Please try one of the following supported formats: #{AvatarUploader::SAFE_IMAGE_EXT.join(', ')}"
  end
end

#uncached_avatar_path(only_path: true, size: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/concerns/avatarable.rb', line 68

def uncached_avatar_path(only_path: true, size: nil)
  return unless self.try(:avatar).present?

  asset_host = ActionController::Base.asset_host
  use_asset_host = asset_host.present?
  use_authentication = respond_to?(:public?) && !public?
  query_params = size&.nonzero? ? "?width=#{size}" : ""

  # 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.
  if use_asset_host && use_authentication
    use_asset_host = false
    only_path = false
  end

  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

  url_base.join + avatar.local_url + query_params
end

#upload_paths(identifier) ⇒ Object

Path that is persisted in the tracking Upload model. Used to fetch the upload from the model.



99
100
101
# File 'app/models/concerns/avatarable.rb', line 99

def upload_paths(identifier)
  avatar_mounter.blank_uploader.store_dirs.map { |store, path| File.join(path, identifier) }
end