OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
made working_directory implicit and added conversion spec
Markus Kahlab2df3d54151
diff --git a/modules/bim/app/services/bim/ifc_models/view_converter_service.rb b/modules/bim/app/services/bim/ifc_models/view_converter_service.rbindex 260495a4a8c..9cb9582bf22 100644--- a/modules/bim/app/services/bim/ifc_models/view_converter_service.rb+++ b/modules/bim/app/services/bim/ifc_models/view_converter_service.rb@@ -58,48 +58,58 @@ module Bimvalidate!Dir.mktmpdir do |dir|- perform_conversion!(dir)+ self.working_directory = dir++ perform_conversion!ServiceResult.new(success: ifc_model.save, result: ifc_model)endrescue StandardError => eOpenProject.logger.error("Failed to convert IFC to XKT", exception: e)ServiceResult.new(success: false).tap { |r| r.errors.add(:base, e.message) }+ ensure+ self.working_directory = nilend- def perform_conversion!(dir)+ def perform_conversion!# Step 0: avoid file name issues (e.g. umlauts) in the pipeline- ifc_path = Pathname(ifc_model.ifc_attachment.diskfile.path)- tmp_ifc_path = Pathname(dir).join("model.ifc")+ tmp_ifc_path = link_to_ifc_file!- FileUtils.symlink ifc_path.to_s, tmp_ifc_path.to_s+ tmp_ifc_path+ .then { |ifc_path| convert_to_collada ifc_path } # Step 1: IfcConvert+ .then { |collada_path| convert_to_gltf collada_path } # Step 2: Collada2GLTF+ .then { |gltf_path| convert_to_xkt gltf_path } # Step 3: Convert to XKT+ .then { |xkt_path| save_xkt xkt_path }- tmp_ifc_path.to_s- .then { |ifc_file| convert_to_collada ifc_file, dir } # Step 1: IfcConvert- .then { |collada_file| convert_to_gltf collada_file, dir } # Step 2: Collada2GLTF- .then { |gltf_file| convert_to_xkt gltf_file, dir } # Step 3: Convert to XKT- .then { |xkt_file| attach_files xkt_file, ifc_path, tmp_ifc_path, dir }+ tmp_ifc_path+ .then { |ifc_path| convert_metadata ifc_path }+ .then { |metadata_path| save_metadata metadata_path }end- def attach_files(xkt_file, ifc_path, tmp_ifc_path, dir)- attach_xkt xkt_file, ifc_path- attach_metadata ifc_path, tmp_ifc_path, dir- end+ def link_to_ifc_file!+ tmp_ifc_path = File.join working_directory, "model.ifc"- def attach_xkt(xkt_file, ifc_path)- final_xkt_file_path = change_basename xkt_file, ifc_path, ".xkt"- FileUtils.mv xkt_file, final_xkt_file_path.to_s+ FileUtils.symlink ifc_model_path.to_s, tmp_ifc_path- ifc_model.xkt_attachment = File.new final_xkt_file_path.to_s+ tmp_ifc_pathend- def attach_metadata(ifc_path, tmp_ifc_path, dir)- metadata_file = convert_metadata tmp_ifc_path.to_s, dir- final_metadata_file_path = change_basename metadata_file, ifc_path, ".json"+ def ifc_model_path+ ifc_path = Pathname(ifc_model.ifc_attachment.diskfile.path)+ end++ def save_xkt(xkt_path)+ final_xkt_path = change_basename xkt_path, ifc_model_path, ".xkt"+ FileUtils.mv xkt_path, final_xkt_path.to_s++ ifc_model.xkt_attachment = File.new final_xkt_path.to_s+ end- FileUtils.mv metadata_file, final_metadata_file_path.to_s+ def save_metadata(metadata_path)+ final_metadata_path = change_basename metadata_path, ifc_model_path, ".json"+ FileUtils.mv metadata_path, final_metadata_path.to_s- ifc_model.metadata_attachment = File.new final_metadata_file_path.to_s+ ifc_model.metadata_attachment = File.new final_metadata_path.to_send##@@ -107,11 +117,10 @@ module Bim# DAE collada file.## @param ifc_filepath {String} Path to the IFC model file- # @param target_dir {String} Path to the temporary output folder- def convert_to_collada(ifc_filepath, target_dir)+ def convert_to_collada(ifc_filepath)Rails.logger.debug { "Converting #{ifc_model.inspect} to DAE" }- convert!(ifc_filepath, target_dir, 'dae') do |target_file|+ convert!(ifc_filepath, 'dae') do |target_file|# To include IfcSpace entities, which by default are excluded by# IfcConvert, together with IfcOpeningElement, we need ot over-# write the default exclude parameter to only exclude@@ -133,11 +142,10 @@ module Bim# Call COLLADA2GLTF with the converted DAE file.## @param dae_filepath {String} Path to the converted DAE model file- # @param target_dir {String} Path to the temporary output folder- def convert_to_gltf(dae_filepath, target_dir)+ def convert_to_gltf(dae_filepath)Rails.logger.debug { "Converting #{ifc_model.inspect} to GLTF" }- convert!(dae_filepath, target_dir, 'gltf') do |target_file|+ convert!(dae_filepath, 'gltf') do |target_file|Open3.capture2e('COLLADA2GLTF', '-i', dae_filepath, '-o', target_file)endend@@ -146,11 +154,10 @@ module Bim# Call gltf2xkt with the converted gltf file.## @param gltf_filepath {String} Path to the converted GLTF model file- # @param target_dir {String} Path to the temporary output folder- def convert_to_xkt(gltf_filepath, target_dir)+ def convert_to_xkt(gltf_filepath)Rails.logger.debug { "Converting #{ifc_model.inspect} to XKT" }- convert!(gltf_filepath, target_dir, 'xkt') do |target_file|+ convert!(gltf_filepath, 'xkt') do |target_file|Open3.capture2e('gltf2xkt', '-s', gltf_filepath, '-o', target_file)endend@@ -159,21 +166,22 @@ module Bim# Call xeokit-metadata## @param ifc_filepath {String} Path to the converted IFC model file- # @param target_dir {String} Path to the temporary output folder- def convert_metadata(ifc_filepath, target_dir)+ def convert_metadata(ifc_filepath)Rails.logger.debug { "Retrieving metadata of #{ifc_model.inspect}" }- convert!(ifc_filepath, target_dir, 'json') do |target_file|+ convert!(ifc_filepath, 'json') do |target_file|Open3.capture2e('xeokit-metadata', ifc_filepath, target_file)endend### Build input filename and target filename- def convert!(source_file, target_dir, ext)+ def convert!(source_file, ext)+ raise ArgumentError, "missing working directory" unless working_directory.present?+filename = File.basename(source_file, '.*')target_filename = "#{filename}.#{ext}"- target_file = File.join(target_dir, target_filename)+ target_file = File.join(working_directory, target_filename)out, status = yield target_file@@ -198,6 +206,16 @@ module BimPathname(from).parent.join(to.basename.to_s.sub(to.extname, ext))end++ private++ def working_directory=(dir)+ @working_directory = dir+ end++ def working_directory+ @working_directory+ endendendenddiff --git a/modules/bim/spec/services/ifc_models/view_conversion_service_spec.rb b/modules/bim/spec/services/ifc_models/view_conversion_service_spec.rbindex c78cc26331f..36513debfbc 100644--- a/modules/bim/spec/services/ifc_models/view_conversion_service_spec.rb+++ b/modules/bim/spec/services/ifc_models/view_conversion_service_spec.rb@@ -59,17 +59,73 @@ describe Bim::IfcModels::ViewConverterService doendcontext 'if available' do+ let(:working_directory) { Dir.mktmpdir }+ let(:ifc_model_file_name) { "büro.ifc" }+ let(:ifc_model_path) { File.join working_directory, ifc_model_file_name }+ let(:ext_regex) { /\.[^\.]*\Z/ }+before doallow(described_class).to receive(:available?).and_return true++ FileUtils.touch ifc_model_path++ allow(subject).to receive(:ifc_model_path).and_return(ifc_model_path)+ allow(subject).to receive(:working_directory).and_return(working_directory)end- it 'calls the conversion and returns save result' do+ after do+ FileUtils.remove_entry working_directory+ end++ it 'performs the conversion and returns the save result' do+ # mocking all convert! calls so they do nothing but create an empty dummy result file+ allow(subject).to receive(:convert!) do |source_file, ext|+ expect(File.exists?(source_file)).to be_truthy, "Expected #{source_file} to exist."++ target_file_path = source_file.sub ext_regex, "." + ext++ FileUtils.touch target_file_path++ target_file_path+ end++ # expect conversion pipeline to start with generic model.ifc and end with+ # büro.xkt based on the original file name+expect(subject)- .to(receive(:perform_conversion!))+ .to receive(:convert_to_collada)+ .with(File.join(working_directory, "model.ifc"))+ .and_call_original++ expect(subject)+ .to receive(:convert_to_gltf)+ .with(File.join(working_directory, "model.dae"))+ .and_call_original++ expect(subject)+ .to receive(:convert_to_xkt)+ .with(File.join(working_directory, "model.gltf"))+ .and_call_originalexpect(model)- .to(receive(:save))- .and_return(true)+ .to receive(:xkt_attachment=) { |file|+ expect(file.path).to end_with(ifc_model_file_name.sub(ext_regex, ".xkt"))+ }++ # expect metadata conversion starting with generic model.ifc and ending+ # with büro.json based on the original file name++ expect(subject)+ .to receive(:convert_metadata)+ .with(File.join(working_directory, "model.ifc"))+ .and_call_original++ expect(model)+ .to receive(:metadata_attachment=) { |file|+ expect(file.path).to end_with(ifc_model_file_name.sub(ext_regex, ".json"))+ }++ expect(model).to receive(:save).and_return(true)expect(subject.call).to be_successend@@ -86,4 +142,13 @@ describe Bim::IfcModels::ViewConverterService doendendend++ describe '#change_basename' do+ it "should return the new basename" do+ path = "/tmp/file.xml"+ new_path = subject.change_basename path, "/home/model.xml", ".json"++ expect(new_path.to_s).to eq "/tmp/model.json"+ end+ endend