Validate size of Account#avatar
This commit is contained in:
parent
d1b5b5d9d5
commit
25a06b1ce1
3 changed files with 12 additions and 0 deletions
|
@ -6,6 +6,8 @@ class Account < ApplicationRecord
|
|||
|
||||
NICKNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze
|
||||
|
||||
AVATAR_MAX_SIZE = 1.megabyte
|
||||
|
||||
AVATAR_CONTENT_TYPES = %w[
|
||||
image/png
|
||||
image/jpeg
|
||||
|
@ -74,6 +76,7 @@ class Account < ApplicationRecord
|
|||
|
||||
validates :biography, allow_nil: true, length: { in: 3..10_000 }
|
||||
|
||||
validate :avatar_size_is_valid
|
||||
validate :avatar_content_type_is_valid
|
||||
|
||||
###########
|
||||
|
@ -135,6 +138,13 @@ private
|
|||
self.biography = biography&.strip
|
||||
end
|
||||
|
||||
def avatar_size_is_valid
|
||||
return unless avatar.attached?
|
||||
return if avatar.blob.byte_size <= AVATAR_MAX_SIZE
|
||||
|
||||
errors.add :avatar, :size
|
||||
end
|
||||
|
||||
def avatar_content_type_is_valid
|
||||
return unless avatar.attached?
|
||||
return if avatar.blob.content_type.in? AVATAR_CONTENT_TYPES
|
||||
|
|
|
@ -112,4 +112,5 @@ en:
|
|||
account:
|
||||
attributes:
|
||||
avatar:
|
||||
size: 'has too big size'
|
||||
format: 'has invalid format: %{content_type}'
|
||||
|
|
|
@ -112,4 +112,5 @@ ru:
|
|||
account:
|
||||
attributes:
|
||||
avatar:
|
||||
size: 'имеет слишком большой размер'
|
||||
format: 'имеет неверный формат: %{content_type}'
|
||||
|
|
Reference in a new issue