Handle upload file size limit in UI

This commit is contained in:
Guillaume Lazzara 2016-02-18 15:46:59 +01:00
parent 534e041e1b
commit a643f34621
7 changed files with 90 additions and 9 deletions

View file

@ -37,7 +37,10 @@ class Users::DescriptionController < UsersController
unless params[:cerfa_pdf].nil? unless params[:cerfa_pdf].nil?
cerfa = @dossier.cerfa cerfa = @dossier.cerfa
cerfa.content = params[:cerfa_pdf] cerfa.content = params[:cerfa_pdf]
cerfa.save unless cerfa.save
flash.now.alert = cerfa.errors.full_messages.join('<br />').html_safe
return render 'show'
end
end end
end end
@ -51,7 +54,10 @@ class Users::DescriptionController < UsersController
@dossier.pieces_justificatives.each do |piece_justificative| @dossier.pieces_justificatives.each do |piece_justificative|
unless params["piece_justificative_#{piece_justificative.type}"].nil? unless params["piece_justificative_#{piece_justificative.type}"].nil?
piece_justificative.content = params["piece_justificative_#{piece_justificative.type}"] 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('<br />').html_safe
return render 'show'
end
end end
end end

View file

@ -2,6 +2,7 @@ class Cerfa < ActiveRecord::Base
belongs_to :dossier belongs_to :dossier
mount_uploader :content, CerfaUploader mount_uploader :content, CerfaUploader
validates :content, :file_size => { :maximum => 3.megabytes.to_i }
def empty? def empty?
content.blank? content.blank?

View file

@ -7,6 +7,7 @@ class PieceJustificative < ActiveRecord::Base
alias_attribute :type, :type_de_piece_justificative_id alias_attribute :type, :type_de_piece_justificative_id
mount_uploader :content, PieceJustificativeUploader mount_uploader :content, PieceJustificativeUploader
validates :content, :file_size => { :maximum => 3.megabytes.to_i }
def empty? def empty?
content.blank? content.blank?

View file

@ -39,7 +39,7 @@ class PieceJustificativeUploader < CarrierWave::Uploader::Base
# Add a white list of extensions which are allowed to be uploaded. # Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this: # For images you might use something like this:
def extension_white_list def extension_white_list
%w(pdf) %w(pdf xlsx xls doc docx)
end end
# Override the filename of the uploaded files: # Override the filename of the uploaded files:

View file

@ -59,15 +59,15 @@
- if @procedure.cerfa_flag - if @procedure.cerfa_flag
%tr %tr
%th{class:'col-lg-6'} %th{class:'col-lg-6'}
='Formulaire (.pdf / .doc)' ='Formulaire (.pdf / .doc / .docx / .xls / .xlsx)'
%td{class:'col-lg-5'} %td{class:'col-lg-5'}
-if @dossier.cerfa_available? -if @dossier.cerfa_available?
%span.btn.btn-sm.btn-file.btn-success %span.btn.btn-sm.btn-file.btn-success
Modifier 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 -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| - @dossier.pieces_justificatives.each do |piece_justificative|
%tr %tr
@ -78,11 +78,11 @@
%span.text-success{ id: "piece_justificative_#{piece_justificative.type}" } Nous l'avons récupéré pour vous. %span.text-success{ id: "piece_justificative_#{piece_justificative.type}" } Nous l'avons récupéré pour vous.
-else -else
-if piece_justificative.empty? -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 -else
%span.btn.btn-sm.btn-file.btn-success %span.btn.btn-sm.btn-file.btn-success
Modifier 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'} %div{style: 'text-align:right'}
%h6 Tous les champs portant un * sont obligatoires. %h6 Tous les champs portant un * sont obligatoires.

View file

@ -41,6 +41,14 @@ fr:
activerecord: activerecord:
errors: errors:
models: 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: user:
attributes: attributes:
email: email:
@ -130,5 +138,5 @@ fr:
procedure_not_found: "La procédure n'existe pas" procedure_not_found: "La procédure n'existe pas"
france_connect: france_connect:
connexion: "Erreur lors de la connexion à 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."

View file

@ -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