Change avatar and header size limits from 2MB to 8MB when using libvips (#33002)

This commit is contained in:
Eugen Rochko 2024-11-26 22:01:52 +01:00 committed by GitHub
parent 7a3dea385e
commit 46c43d263c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 18 deletions

View file

@ -3,9 +3,8 @@
module Account::Avatar module Account::Avatar
extend ActiveSupport::Concern extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze AVATAR_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
LIMIT = 2.megabytes AVATAR_LIMIT = Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes
AVATAR_DIMENSIONS = [400, 400].freeze AVATAR_DIMENSIONS = [400, 400].freeze
AVATAR_GEOMETRY = [AVATAR_DIMENSIONS.first, AVATAR_DIMENSIONS.last].join('x') AVATAR_GEOMETRY = [AVATAR_DIMENSIONS.first, AVATAR_DIMENSIONS.last].join('x')
@ -22,9 +21,9 @@ module Account::Avatar
included do included do
# Avatar upload # 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] 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_content_type :avatar, content_type: AVATAR_IMAGE_MIME_TYPES
validates_attachment_size :avatar, less_than: LIMIT validates_attachment_size :avatar, less_than: AVATAR_LIMIT
remotable_attachment :avatar, LIMIT, suppress_errors: false remotable_attachment :avatar, AVATAR_LIMIT, suppress_errors: false
end end
def avatar_original_url def avatar_original_url

View file

@ -3,16 +3,15 @@
module Account::Header module Account::Header
extend ActiveSupport::Concern extend ActiveSupport::Concern
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze HEADER_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
LIMIT = 2.megabytes HEADER_LIMIT = Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes
HEADER_DIMENSIONS = [1500, 500].freeze HEADER_DIMENSIONS = [1500, 500].freeze
HEADER_GEOMETRY = [HEADER_DIMENSIONS.first, HEADER_DIMENSIONS.last].join('x') 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 class_methods do
def header_styles(file) 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[:static] = { format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif'
styles styles
end end
@ -23,9 +22,9 @@ module Account::Header
included do included do
# Header upload # 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] 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_content_type :header, content_type: HEADER_IMAGE_MIME_TYPES
validates_attachment_size :header, less_than: LIMIT validates_attachment_size :header, less_than: HEADER_LIMIT
remotable_attachment :header, LIMIT, suppress_errors: false remotable_attachment :header, HEADER_LIMIT, suppress_errors: false
end end
def header_original_url def header_original_url

View file

@ -34,8 +34,8 @@
.fields-row__column.fields-row__column-6 .fields-row__column.fields-row__column-6
.fields-group .fields-group
= f.input :avatar, = f.input :avatar,
hint: t('simple_form.hints.defaults.avatar', dimensions: Account::Avatar::AVATAR_GEOMETRY, size: number_to_human_size(Account::Avatar::LIMIT)), 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::IMAGE_MIME_TYPES.join(',') }, input_html: { accept: Account::Avatar::AVATAR_IMAGE_MIME_TYPES.join(',') },
wrapper: :with_block_label wrapper: :with_block_label
.fields-row__column.fields-row__column-6 .fields-row__column.fields-row__column-6
@ -50,8 +50,8 @@
.fields-row__column.fields-row__column-6 .fields-row__column.fields-row__column-6
.fields-group .fields-group
= f.input :header, = f.input :header,
hint: t('simple_form.hints.defaults.header', dimensions: Account::Header::HEADER_GEOMETRY, size: number_to_human_size(Account::Header::LIMIT)), 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::IMAGE_MIME_TYPES.join(',') }, input_html: { accept: Account::Header::HEADER_IMAGE_MIME_TYPES.join(',') },
wrapper: :with_block_label wrapper: :with_block_label
.fields-row__column.fields-row__column-6 .fields-row__column.fields-row__column-6