From d9cef43d822b448d89f248721c288d96f23e58fd Mon Sep 17 00:00:00 2001 From: ulferts Date: Thu, 23 Jan 2020 10:17:19 +0100 Subject: [PATCH] check for ifc file conformity --- .../app/contracts/ifc_models/base_contract.rb | 15 ++++++++++ modules/ifc_models/config/locales/en.yml | 3 +- .../ifc_models/shared_contract_examples.rb | 28 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb index c15176edbb..9be2f216f4 100644 --- a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb +++ b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb @@ -44,6 +44,7 @@ module IFCModels user_allowed_to_manage user_is_uploader ifc_attachment_existent + ifc_attachment_is_ifc uploader_is_ifc_attachment_author super @@ -65,6 +66,20 @@ module IFCModels errors.add :base, :ifc_attachment_missing unless model.ifc_attachment end + def ifc_attachment_is_ifc + return unless model.ifc_attachment&.new_record? + + firstline = File.open(model.ifc_attachment.file.file.path, &:readline) + + begin + unless firstline.match?(/^ISO-10303-21;/) + errors.add :base, :invalid_ifc_file + end + rescue ArgumentError + errors.add :base, :invalid_ifc_file + end + end + def uploader_is_ifc_attachment_author errors.add :uploader_id, :invalid if model.ifc_attachment && model.uploader != model.ifc_attachment.author end diff --git a/modules/ifc_models/config/locales/en.yml b/modules/ifc_models/config/locales/en.yml index 76c1271f5b..4a5add6068 100644 --- a/modules/ifc_models/config/locales/en.yml +++ b/modules/ifc_models/config/locales/en.yml @@ -41,4 +41,5 @@ en: ifc_models/ifc_model: attributes: base: - ifc_attachment_missing: "No ifc file attached" + ifc_attachment_missing: "No ifc file attached." + invalid_ifc_file: "The provided file is not a valid IFC file." diff --git a/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb b/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb index cbcc33c467..6eb3632e5d 100644 --- a/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb +++ b/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb @@ -106,6 +106,34 @@ shared_examples_for 'ifc model contract' do end end + context 'if the new ifc file is no valid ifc file' do + let(:ifc_file) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true } + let(:ifc_attachment) do + User.execute_as current_user do + ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' }) + ifc_model.attachments.last + end + end + + it 'is invalid' do + expect_valid(false, base: %i(invalid_ifc_file)) + end + end + + context 'if the new ifc file is a valid ifc file' do + let(:ifc_file) do + FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true, content: "ISO-10303-21;" + end + let(:ifc_attachment) do + User.execute_as current_user do + ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' }) + ifc_model.attachments.last + end + end + + it_behaves_like 'is valid' + end + context 'if user is not allowed to manage ifc models' do let(:permissions) { [] }