ajout de la liste des extensions

This commit is contained in:
clemkeirua 2020-07-16 09:58:07 +02:00 committed by Keirua (Rebase PR Action)
parent a465bad405
commit c155de1d7e
6 changed files with 102 additions and 5 deletions

View file

@ -0,0 +1,47 @@
module ContentTypeHelper
def file_descriptions_from_content_types(content_types)
content_types.map { |ct| CONTENT_TYPE_DESCRIPTIONS[ct] }.uniq.join(', ')
end
def file_extensions_from_content_types(content_types)
content_types.map do |ct|
if CONTENT_TYPE_EXTENSIONS.include?(ct)
".#{CONTENT_TYPE_EXTENSIONS[ct]}"
end
end.uniq.sort.join(', ')
end
CONTENT_TYPE_DESCRIPTIONS = {
"text/plain" => 'Word',
"application/pdf" => "PDF",
"application/msword" => "Word",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "Excel",
"application/vnd.ms-excel" => "Excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "Excel",
"application/vnd.ms-powerpoint" => "PowerPoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => "PowerPoint",
"application/vnd.oasis.opendocument.text" => "LibreOffice",
"application/vnd.oasis.opendocument.presentation" => "LibreOffice",
"application/vnd.oasis.opendocument.spreadsheet" => "LibreOffice",
"image/png" => "PNG",
"image/jpeg" => "JPG"
}
#  Mapping between format and extension documented on MDN:
# https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
CONTENT_TYPE_EXTENSIONS = {
"text/plain" => "txt",
"application/pdf" => "pdf",
"application/msword" => "doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",
"application/vnd.ms-excel" => "xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
"application/vnd.ms-powerpoint" => "ppt",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => "pptx",
"application/vnd.oasis.opendocument.text" => "odt",
"application/vnd.oasis.opendocument.presentation" => "odp",
"application/vnd.oasis.opendocument.spreadsheet" => "ods",
"image/png" => "png",
"image/jpeg" => "jpg"
}
end

View file

@ -17,7 +17,7 @@
class Champs::PieceJustificativeChamp < Champ
MAX_SIZE = 200.megabytes
ACCEPTED_FORMATS = [
ACCEPTED_CONTENT_TYPES = [
"text/plain",
"application/pdf",
"application/msword",
@ -34,7 +34,7 @@ class Champs::PieceJustificativeChamp < Champ
]
validates :piece_justificative_file,
content_type: ACCEPTED_FORMATS,
content_type: ACCEPTED_CONTENT_TYPES,
size: { less_than: MAX_SIZE },
if: -> { !type_de_champ.skip_pj_validation }

View file

@ -31,6 +31,11 @@
= button_tag type: 'button', class: 'button attachment-error-retry', data: { 'input-target': ".attachment-input-#{attachment_id}" } do
%span.icon.retry
Ré-essayer
- if defined?(allowed_content_types) && allowed_content_types.present?
%p.mb-1
Formats acceptés : #{file_descriptions_from_content_types(allowed_content_types)}
%sup
%span.icon.info{ :title => "Cela correspond aux fichiers ayant comme extension #{file_extensions_from_content_types(allowed_content_types)}" }
= form.file_field attached_file.name,
class: "attachment-input attachment-input-#{attachment_id} #{'hidden' if persisted}",

View file

@ -1,4 +1,5 @@
= render 'shared/attachment/edit',
{ form: form,
attached_file: champ.piece_justificative_file,
template: champ.type_de_champ.piece_justificative_template, user_can_destroy: true }
template: champ.type_de_champ.piece_justificative_template, user_can_destroy: true,
allowed_content_types: Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES }