Allow adjusting avatar sizes (local and remote) via config

This commit is contained in:
Jeremy Kescher 2022-07-20 18:03:40 +02:00
parent 8f969312f7
commit df02b66295
No known key found for this signature in database
GPG key ID: 48DFE4BB15BA5940
3 changed files with 12 additions and 2 deletions

View file

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

View file

@ -286,6 +286,13 @@ MAX_POLL_OPTION_CHARS=100
# MAX_EMOJI_SIZE=262144 # MAX_EMOJI_SIZE=262144
# MAX_REMOTE_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 # Optional hCaptcha support
# HCAPTCHA_SECRET_KEY= # HCAPTCHA_SECRET_KEY=
# HCAPTCHA_SITE_KEY= # HCAPTCHA_SITE_KEY=

View file

@ -4,7 +4,8 @@ module AccountAvatar
extend ActiveSupport::Concern extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze 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 class_methods do
def avatar_styles(file) def avatar_styles(file)
@ -20,7 +21,8 @@ module AccountAvatar
# Avatar upload # Avatar upload
has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail] 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_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 remotable_attachment :avatar, LIMIT, suppress_errors: false
end end