Skip to content

Commit

Permalink
Change avatar and header size limits from 2MB to 8MB when using libvi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Nov 26, 2024
1 parent 7a3dea3 commit 46c43d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
11 changes: 5 additions & 6 deletions app/models/concerns/account/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -22,9 +21,9 @@ def avatar_styles(file)
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
Expand Down
15 changes: 7 additions & 8 deletions app/models/concerns/account/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,9 +22,9 @@ def header_styles(file)
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
Expand Down
8 changes: 4 additions & 4 deletions app/views/settings/profiles/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 46c43d2

Please sign in to comment.