From df02b6629527f7ff64bde1d4d3033d8d694a4b88 Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Wed, 20 Jul 2022 18:03:40 +0200 Subject: [PATCH] Allow adjusting avatar sizes (local and remote) via config --- .env.production.catcatnya | 1 + .env.production.sample | 7 +++++++ app/models/concerns/account_avatar.rb | 6 ++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.env.production.catcatnya b/.env.production.catcatnya index 6d572f5310..9e5a275fb2 100644 --- a/.env.production.catcatnya +++ b/.env.production.catcatnya @@ -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 diff --git a/.env.production.sample b/.env.production.sample index ad058b2b79..2b56299955 100644 --- a/.env.production.sample +++ b/.env.production.sample @@ -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= diff --git a/app/models/concerns/account_avatar.rb b/app/models/concerns/account_avatar.rb index 0cfd9167cd..164ae98d2e 100644 --- a/app/models/concerns/account_avatar.rb +++ b/app/models/concerns/account_avatar.rb @@ -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