Merge pull request #6667 from betagouv/active-storage-validations-max-size

Simplification des messages d’erreur en cas de fichier trop volumineux
This commit is contained in:
Paul Chavard 2021-11-24 13:10:14 +03:00 committed by GitHub
commit 71c54a226d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 23 additions and 47 deletions

View file

@ -67,8 +67,11 @@ GEM
activemodel (>= 4.1, < 6.2) activemodel (>= 4.1, < 6.2)
case_transform (>= 0.2) case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3) jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
active_storage_validations (0.9.2) active_storage_validations (0.9.6)
rails (>= 5.2.0) activejob (>= 5.2.0)
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (6.1.4.1) activejob (6.1.4.1)
activesupport (= 6.1.4.1) activesupport (= 6.1.4.1)
globalid (>= 0.3.6) globalid (>= 0.3.6)

View file

@ -14,7 +14,6 @@
class AttestationTemplate < ApplicationRecord class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
include FileValidationConcern
belongs_to :procedure, optional: false belongs_to :procedure, optional: false
@ -24,8 +23,8 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 } validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE = 1.megabytes FILE_MAX_SIZE = 1.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE) validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE }
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE) validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE }
DOSSIER_STATE = Dossier.states.fetch(:accepte) DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -17,7 +17,6 @@
# #
class Avis < ApplicationRecord class Avis < ApplicationRecord
include EmailSanitizableConcern include EmailSanitizableConcern
include FileValidationConcern
belongs_to :dossier, inverse_of: :avis, touch: true, optional: false belongs_to :dossier, inverse_of: :avis, touch: true, optional: false
belongs_to :experts_procedure, optional: false belongs_to :experts_procedure, optional: false
@ -31,16 +30,16 @@ class Avis < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation(FILE_MAX_SIZE) size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, validates :introduction_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation(FILE_MAX_SIZE) size: { less_than: FILE_MAX_SIZE }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true validates :claimant, presence: true
validates :piece_justificative_file, size: file_size_validation(FILE_MAX_SIZE) validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, size: file_size_validation(FILE_MAX_SIZE) validates :introduction_file, size: { less_than: FILE_MAX_SIZE }
before_validation -> { sanitize_email(:email) } before_validation -> { sanitize_email(:email) }
default_scope { joins(:dossier) } default_scope { joins(:dossier) }

View file

@ -20,11 +20,10 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::PieceJustificativeChamp < Champ class Champs::PieceJustificativeChamp < Champ
include FileValidationConcern
FILE_MAX_SIZE = 200.megabytes FILE_MAX_SIZE = 200.megabytes
validates :piece_justificative_file, validates :piece_justificative_file,
size: file_size_validation(FILE_MAX_SIZE), size: { less_than: FILE_MAX_SIZE },
if: -> { !type_de_champ.skip_pj_validation } if: -> { !type_de_champ.skip_pj_validation }
validates :piece_justificative_file, validates :piece_justificative_file,

View file

@ -20,20 +20,9 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::TitreIdentiteChamp < Champ class Champs::TitreIdentiteChamp < Champ
include FileValidationConcern
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
ACCEPTED_FORMATS = [ validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }
"image/png",
"image/jpeg"
]
# TODO: once we're running on Rails 6, re-enable this validation.
# See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926
#
validates :piece_justificative_file,
content_type: ACCEPTED_FORMATS,
size: file_size_validation(FILE_MAX_SIZE)
def main_value_name def main_value_name
:piece_justificative_file :piece_justificative_file

View file

@ -13,7 +13,6 @@
# instructeur_id :bigint # instructeur_id :bigint
# #
class Commentaire < ApplicationRecord class Commentaire < ApplicationRecord
include FileValidationConcern
include Discard::Model include Discard::Model
self.ignored_columns = [:user_id] self.ignored_columns = [:user_id]
@ -31,7 +30,7 @@ class Commentaire < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe, validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation(FILE_MAX_SIZE) size: { less_than: FILE_MAX_SIZE }
default_scope { order(created_at: :asc) } default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }

View file

@ -1,12 +0,0 @@
module FileValidationConcern
extend ActiveSupport::Concern
class_methods do
# This method works around missing `%{min_size}` and `%{max_size}` variables in active_record_validation
# default error message.
#
# Hopefully this will be fixed upstream in https://github.com/igorkasyanchuk/active_storage_validations/pull/134
def file_size_validation(file_max_size = 200.megabytes)
{ less_than: file_max_size, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(file_max_size)) }
end
end
end

View file

@ -54,7 +54,6 @@ class Procedure < ApplicationRecord
self.ignored_columns = [:duree_conservation_dossiers_hors_ds] self.ignored_columns = [:duree_conservation_dossiers_hors_ds]
include ProcedureStatsConcern include ProcedureStatsConcern
include EncryptableConcern include EncryptableConcern
include FileValidationConcern
include Discard::Model include Discard::Model
self.discard_column = :hidden_at self.discard_column = :hidden_at
@ -253,7 +252,7 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 2, 28) } ], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 2, 28) }
validates :deliberation, content_type: [ validates :deliberation, content_type: [
"application/msword", "application/msword",
@ -264,11 +263,11 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "text/plain"
], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 4, 29) } ], size: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 4, 29) }
LOGO_MAX_SIZE = 5.megabytes LOGO_MAX_SIZE = 5.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'],
size: file_size_validation(LOGO_MAX_SIZE), size: { less_than: LOGO_MAX_SIZE },
if: -> { new_record? || created_at > Date.new(2020, 11, 13) } if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true validates :api_entreprise_token, jwt_token: true, allow_blank: true

View file

@ -101,6 +101,7 @@ ignore_unused:
- 'activerecord.errors.*' - 'activerecord.errors.*'
- 'errors.messages.blank' - 'errors.messages.blank'
- 'errors.messages.content_type_invalid' - 'errors.messages.content_type_invalid'
- 'errors.messages.file_size_out_of_range'
- 'pluralize.*' - 'pluralize.*'
- 'views.pagination.*' - 'views.pagination.*'
- 'time.formats.default' - 'time.formats.default'

View file

@ -1,5 +1,5 @@
en: en:
errors: errors:
messages: messages:
content_type_invalid: "is not of an accepted type" content_type_invalid: is not of an accepted type
file_size_out_of_range: "is too big. The file must be at most %{file_size_limit}." file_size_out_of_range: is too big. The file must be at most %{max_size}.

View file

@ -1,5 +1,5 @@
fr: fr:
errors: errors:
messages: messages:
content_type_invalid: "nest pas dun type accepté" content_type_invalid: nest pas dun type accepté
file_size_out_of_range: "est trop lourd(e). Le fichier doit faire au plus %{file_size_limit}." file_size_out_of_range: est trop lourd(e). Le fichier doit faire au plus %{max_size}.