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 }

View file

@ -0,0 +1,44 @@
RSpec.describe ContentTypeHelper, type: :helper do
describe ".file_descriptions_from_content_types" do
subject { file_descriptions_from_content_types(content_types) }
context "when a content-type does not exist" do
let(:content_types) { ['invalid content type'] }
it { is_expected.to eq("") }
end
context "when a content type is valid" do
let(:content_types) { ["application/msword"] }
it { is_expected.to eq 'Word' }
end
context "when two content types have the same description" do
let(:content_types) { ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-excel"] }
it { is_expected.to eq 'Excel' }
end
context "when two content types have different descriptions" do
let(:content_types) { ["application/msword", "application/vnd.ms-excel"] }
it { is_expected.to eq 'Word, Excel' }
end
end
describe ".file_extensions_from_content_types" do
subject { file_extensions_from_content_types(content_types) }
context "when a content-type does not exist" do
let(:content_types) { ['invalid content type'] }
it { is_expected.to eq("") }
end
context "when a content type is valid" do
let(:content_types) { ["application/msword"] }
it { is_expected.to eq '.doc' }
end
context "when two content types have different extensions" do
let(:content_types) { ["application/vnd.ms-excel", "application/msword"] }
it { is_expected.to eq '.doc, .xls' }
end
end
end

View file

@ -22,7 +22,7 @@ describe Champs::PieceJustificativeChamp do
context "by default" do
it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
it { is_expected.to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_FORMATS) }
it { is_expected.to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES) }
it { expect(champ_pj.type_de_champ.skip_pj_validation).to be_falsy }
end
@ -30,7 +30,7 @@ describe Champs::PieceJustificativeChamp do
before { champ_pj.type_de_champ.update(skip_pj_validation: true) }
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
it { is_expected.not_to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_FORMATS) }
it { is_expected.not_to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_CONTENT_TYPES) }
end
end