Allow adjusting avatar sizes (local and remote) via config

develop
Jeremy Kescher 2 years ago
parent 8f969312f7
commit df02b66295
No known key found for this signature in database
GPG Key ID: 48DFE4BB15BA5940

@ -40,4 +40,5 @@ MAX_DISPLAY_NAME_CHARS=50
MAX_POLL_OPTIONS=20
MAX_SEARCH_RESULTS=1000
MAX_REMOTE_EMOJI_SIZE=1048576
# MAX_REMOTE_AVATAR_SIZE=4194304
IP_RETENTION_PERIOD=86400

@ -286,6 +286,13 @@ MAX_POLL_OPTION_CHARS=100
# MAX_EMOJI_SIZE=262144
# MAX_REMOTE_EMOJI_SIZE=262144
# Maximum avatar (upload) sizes
# If undefined or smaller than MAX_AVATAR_SIZE, the value
# of MAX_AVATAR_SIZE will be used for MAX_REMOTE_AVATAR_SIZE
# Units are in bytes
# MAX_AVATAR_SIZE=2097152
# MAX_REMOTE_AVATAR_SIZE=2097152
# Optional hCaptcha support
# HCAPTCHA_SECRET_KEY=
# HCAPTCHA_SITE_KEY=

@ -4,7 +4,8 @@ module AccountAvatar
extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
LIMIT = 2.megabytes
LOCAL_LIMIT = (ENV['MAX_AVATAR_SIZE'] || 2.megabytes).to_i
LIMIT = [LOCAL_LIMIT, (ENV['MAX_REMOTE_AVATAR_SIZE'] || 2.megabytes).to_i].max
class_methods do
def avatar_styles(file)
@ -20,7 +21,8 @@ module AccountAvatar
# Avatar upload
has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail]
validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
validates_attachment_size :avatar, less_than: LIMIT
validates_attachment_size :avatar, less_than: LIMIT, unless: :local?
validates_attachment_size :avatar, less_than: LOCAL_LIMIT, if: :local?
remotable_attachment :avatar, LIMIT, suppress_errors: false
end

Loading…
Cancel
Save