From 46c43d263c44d2ae1e896bca3dd8b0195989240d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 26 Nov 2024 22:01:52 +0100 Subject: [PATCH] Change avatar and header size limits from 2MB to 8MB when using libvips (#33002) --- app/models/concerns/account/avatar.rb | 11 +++++------ app/models/concerns/account/header.rb | 15 +++++++-------- app/views/settings/profiles/show.html.haml | 8 ++++---- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/models/concerns/account/avatar.rb b/app/models/concerns/account/avatar.rb index 5ca8fa862f..a60a289d5b 100644 --- a/app/models/concerns/account/avatar.rb +++ b/app/models/concerns/account/avatar.rb @@ -3,9 +3,8 @@ module Account::Avatar extend ActiveSupport::Concern - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze - LIMIT = 2.megabytes - + AVATAR_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze + AVATAR_LIMIT = Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes AVATAR_DIMENSIONS = [400, 400].freeze AVATAR_GEOMETRY = [AVATAR_DIMENSIONS.first, AVATAR_DIMENSIONS.last].join('x') @@ -22,9 +21,9 @@ module Account::Avatar included do # Avatar upload has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '+profile "!icc,*" +set date:modify +set date:create +set date:timestamp' }, processors: [:lazy_thumbnail] - validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES - validates_attachment_size :avatar, less_than: LIMIT - remotable_attachment :avatar, LIMIT, suppress_errors: false + validates_attachment_content_type :avatar, content_type: AVATAR_IMAGE_MIME_TYPES + validates_attachment_size :avatar, less_than: AVATAR_LIMIT + remotable_attachment :avatar, AVATAR_LIMIT, suppress_errors: false end def avatar_original_url diff --git a/app/models/concerns/account/header.rb b/app/models/concerns/account/header.rb index 2a47097fcf..662ee7caf7 100644 --- a/app/models/concerns/account/header.rb +++ b/app/models/concerns/account/header.rb @@ -3,16 +3,15 @@ module Account::Header extend ActiveSupport::Concern - IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze - LIMIT = 2.megabytes - + HEADER_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze + HEADER_LIMIT = Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes HEADER_DIMENSIONS = [1500, 500].freeze HEADER_GEOMETRY = [HEADER_DIMENSIONS.first, HEADER_DIMENSIONS.last].join('x') - MAX_PIXELS = HEADER_DIMENSIONS.first * HEADER_DIMENSIONS.last + HEADER_MAX_PIXELS = HEADER_DIMENSIONS.first * HEADER_DIMENSIONS.last class_methods do def header_styles(file) - styles = { original: { pixels: MAX_PIXELS, file_geometry_parser: FastGeometryParser } } + styles = { original: { pixels: HEADER_MAX_PIXELS, file_geometry_parser: FastGeometryParser } } styles[:static] = { format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif' styles end @@ -23,9 +22,9 @@ module Account::Header included do # Header upload has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '+profile "!icc,*" +set date:modify +set date:create +set date:timestamp' }, processors: [:lazy_thumbnail] - validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES - validates_attachment_size :header, less_than: LIMIT - remotable_attachment :header, LIMIT, suppress_errors: false + validates_attachment_content_type :header, content_type: HEADER_IMAGE_MIME_TYPES + validates_attachment_size :header, less_than: HEADER_LIMIT + remotable_attachment :header, HEADER_LIMIT, suppress_errors: false end def header_original_url diff --git a/app/views/settings/profiles/show.html.haml b/app/views/settings/profiles/show.html.haml index 427a4fa95a..3815eb7fc2 100644 --- a/app/views/settings/profiles/show.html.haml +++ b/app/views/settings/profiles/show.html.haml @@ -34,8 +34,8 @@ .fields-row__column.fields-row__column-6 .fields-group = f.input :avatar, - hint: t('simple_form.hints.defaults.avatar', dimensions: Account::Avatar::AVATAR_GEOMETRY, size: number_to_human_size(Account::Avatar::LIMIT)), - input_html: { accept: Account::Avatar::IMAGE_MIME_TYPES.join(',') }, + hint: t('simple_form.hints.defaults.avatar', dimensions: Account::Avatar::AVATAR_GEOMETRY, size: number_to_human_size(Account::Avatar::AVATAR_LIMIT)), + input_html: { accept: Account::Avatar::AVATAR_IMAGE_MIME_TYPES.join(',') }, wrapper: :with_block_label .fields-row__column.fields-row__column-6 @@ -50,8 +50,8 @@ .fields-row__column.fields-row__column-6 .fields-group = f.input :header, - hint: t('simple_form.hints.defaults.header', dimensions: Account::Header::HEADER_GEOMETRY, size: number_to_human_size(Account::Header::LIMIT)), - input_html: { accept: Account::Header::IMAGE_MIME_TYPES.join(',') }, + hint: t('simple_form.hints.defaults.header', dimensions: Account::Header::HEADER_GEOMETRY, size: number_to_human_size(Account::Header::HEADER_LIMIT)), + input_html: { accept: Account::Header::HEADER_IMAGE_MIME_TYPES.join(',') }, wrapper: :with_block_label .fields-row__column.fields-row__column-6