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)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
active_storage_validations (0.9.2)
rails (>= 5.2.0)
active_storage_validations (0.9.6)
activejob (>= 5.2.0)
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (6.1.4.1)
activesupport (= 6.1.4.1)
globalid (>= 0.3.6)

View file

@ -14,7 +14,6 @@
class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern
include FileValidationConcern
belongs_to :procedure, optional: false
@ -24,8 +23,8 @@ class AttestationTemplate < ApplicationRecord
validates :footer, length: { maximum: 190 }
FILE_MAX_SIZE = 1.megabytes
validates :logo, 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: 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: { less_than: FILE_MAX_SIZE }
DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -17,7 +17,6 @@
#
class Avis < ApplicationRecord
include EmailSanitizableConcern
include FileValidationConcern
belongs_to :dossier, inverse_of: :avis, touch: true, optional: false
belongs_to :experts_procedure, optional: false
@ -31,16 +30,16 @@ class Avis < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes
validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation(FILE_MAX_SIZE)
size: { less_than: FILE_MAX_SIZE }
validates :introduction_file,
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 :claimant, presence: true
validates :piece_justificative_file, size: file_size_validation(FILE_MAX_SIZE)
validates :introduction_file, size: file_size_validation(FILE_MAX_SIZE)
validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, size: { less_than: FILE_MAX_SIZE }
before_validation -> { sanitize_email(:email) }
default_scope { joins(:dossier) }

View file

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

View file

@ -20,20 +20,9 @@
# type_de_champ_id :integer
#
class Champs::TitreIdentiteChamp < Champ
include FileValidationConcern
FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = [
"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)
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }
def main_value_name
:piece_justificative_file

View file

@ -13,7 +13,6 @@
# instructeur_id :bigint
#
class Commentaire < ApplicationRecord
include FileValidationConcern
include Discard::Model
self.ignored_columns = [:user_id]
@ -31,7 +30,7 @@ class Commentaire < ApplicationRecord
FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES,
size: file_size_validation(FILE_MAX_SIZE)
size: { less_than: FILE_MAX_SIZE }
default_scope { order(created_at: :asc) }
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]
include ProcedureStatsConcern
include EncryptableConcern
include FileValidationConcern
include Discard::Model
self.discard_column = :hidden_at
@ -253,7 +252,7 @@ class Procedure < ApplicationRecord
"image/jpg",
"image/png",
"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: [
"application/msword",
@ -264,11 +263,11 @@ class Procedure < ApplicationRecord
"image/jpg",
"image/png",
"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
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) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true

View file

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

View file

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

View file

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