From a643f34621411cc6b51e3b985d37089dab6cbc18 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Thu, 18 Feb 2016 15:46:59 +0100 Subject: [PATCH] Handle upload file size limit in UI --- .../users/description_controller.rb | 10 ++- app/models/cerfa.rb | 1 + app/models/piece_justificative.rb | 1 + app/uploaders/piece_justificative_uploader.rb | 2 +- app/views/users/description/show.html.haml | 10 +-- config/locales/fr.yml | 10 ++- lib/file_size_validator.rb | 65 +++++++++++++++++++ 7 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 lib/file_size_validator.rb diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 8e3e0f912..212973185 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -37,7 +37,10 @@ class Users::DescriptionController < UsersController unless params[:cerfa_pdf].nil? cerfa = @dossier.cerfa cerfa.content = params[:cerfa_pdf] - cerfa.save + unless cerfa.save + flash.now.alert = cerfa.errors.full_messages.join('
').html_safe + return render 'show' + end end end @@ -51,7 +54,10 @@ class Users::DescriptionController < UsersController @dossier.pieces_justificatives.each do |piece_justificative| unless params["piece_justificative_#{piece_justificative.type}"].nil? piece_justificative.content = params["piece_justificative_#{piece_justificative.type}"] - piece_justificative.save + unless piece_justificative.save + flash.now.alert = piece_justificative.errors.full_messages.join('
').html_safe + return render 'show' + end end end diff --git a/app/models/cerfa.rb b/app/models/cerfa.rb index a64d21d16..e22ec0d35 100644 --- a/app/models/cerfa.rb +++ b/app/models/cerfa.rb @@ -2,6 +2,7 @@ class Cerfa < ActiveRecord::Base belongs_to :dossier mount_uploader :content, CerfaUploader + validates :content, :file_size => { :maximum => 3.megabytes.to_i } def empty? content.blank? diff --git a/app/models/piece_justificative.rb b/app/models/piece_justificative.rb index bb9f15e9d..c72b886a9 100644 --- a/app/models/piece_justificative.rb +++ b/app/models/piece_justificative.rb @@ -7,6 +7,7 @@ class PieceJustificative < ActiveRecord::Base alias_attribute :type, :type_de_piece_justificative_id mount_uploader :content, PieceJustificativeUploader + validates :content, :file_size => { :maximum => 3.megabytes.to_i } def empty? content.blank? diff --git a/app/uploaders/piece_justificative_uploader.rb b/app/uploaders/piece_justificative_uploader.rb index 1f3d8c317..01021b1c5 100644 --- a/app/uploaders/piece_justificative_uploader.rb +++ b/app/uploaders/piece_justificative_uploader.rb @@ -39,7 +39,7 @@ class PieceJustificativeUploader < CarrierWave::Uploader::Base # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_list - %w(pdf) + %w(pdf xlsx xls doc docx) end # Override the filename of the uploaded files: diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index 77a258a71..f044512a1 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -59,15 +59,15 @@ - if @procedure.cerfa_flag %tr %th{class:'col-lg-6'} - ='Formulaire (.pdf / .doc)' + ='Formulaire (.pdf / .doc / .docx / .xls / .xlsx)' %td{class:'col-lg-5'} -if @dossier.cerfa_available? %span.btn.btn-sm.btn-file.btn-success Modifier - %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: ".pdf"} + %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: "application/pdf", :max_file_size => 3.megabytes } -else - %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: ".pdf"} + %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: "application/pdf", :max_file_size => 3.megabytes } - @dossier.pieces_justificatives.each do |piece_justificative| %tr @@ -78,11 +78,11 @@ %span.text-success{ id: "piece_justificative_#{piece_justificative.type}" } Nous l'avons récupéré pour vous. -else -if piece_justificative.empty? - = file_field_tag "piece_justificative_#{piece_justificative.type}", accept: '.pdf' + = file_field_tag "piece_justificative_#{piece_justificative.type}", accept: 'application/pdf', :max_file_size => 3.megabytes -else %span.btn.btn-sm.btn-file.btn-success Modifier - = file_field_tag "piece_justificative_#{piece_justificative.type}", accept: '.pdf' + = file_field_tag "piece_justificative_#{piece_justificative.type}", accept: 'application/pdf', :max_file_size => 3.megabytes %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 20699080f..f38143913 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -41,6 +41,14 @@ fr: activerecord: errors: models: + piece_justificative: + attributes: + content: + size_too_big: "La taille du fichier joint est trop importante. Elle doit être inférieure à 3Mo." + cerfa: + attributes: + content: + size_too_big: "La taille du fichier joint est trop importante. Elle doit être inférieure à 3Mo." user: attributes: email: @@ -130,5 +138,5 @@ fr: procedure_not_found: "La procédure n'existe pas" france_connect: connexion: "Erreur lors de la connexion à France Connect." - + extension_white_list_error: "Le format de fichier de la pièce jointe n'est pas valide." diff --git a/lib/file_size_validator.rb b/lib/file_size_validator.rb new file mode 100644 index 000000000..9a79b3424 --- /dev/null +++ b/lib/file_size_validator.rb @@ -0,0 +1,65 @@ +class FileSizeValidator < ActiveModel::EachValidator + MESSAGES = { :is => :wrong_size, :minimum => :size_too_small, :maximum => :size_too_big }.freeze + CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze + + DEFAULT_TOKENIZER = lambda { |value| value.split(//) } + RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long] + + def initialize(options) + if range = (options.delete(:in) || options.delete(:within)) + raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range) + options[:minimum], options[:maximum] = range.begin, range.end + options[:maximum] -= 1 if range.exclude_end? + end + + super + end + + def check_validity! + keys = CHECKS.keys & options.keys + + if keys.empty? + raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.' + end + + keys.each do |key| + value = options[key] + + unless value.is_a?(Integer) && value >= 0 + raise ArgumentError, ":#{key} must be a nonnegative Integer" + end + end + end + + def validate_each(record, attribute, value) + raise(ArgumentError, "A CarrierWave::Uploader::Base object was expected") unless value.kind_of? CarrierWave::Uploader::Base + + value = (options[:tokenizer] || DEFAULT_TOKENIZER).call(value) if value.kind_of?(String) + + CHECKS.each do |key, validity_check| + next unless check_value = options[key] + + value ||= [] if key == :maximum + + value_size = value.size + next if value_size.send(validity_check, check_value) + + errors_options = options.except(*RESERVED_OPTIONS) + errors_options[:file_size] = help.number_to_human_size check_value + + default_message = options[MESSAGES[key]] + errors_options[:message] ||= default_message if default_message + + record.errors.add(attribute, MESSAGES[key], errors_options) + end + end + + def help + Helper.instance + end + + class Helper + include Singleton + include ActionView::Helpers::NumberHelper + end +end \ No newline at end of file