diff --git a/app/jobs/process_uploaded_file_job.rb b/app/jobs/process_uploaded_file_job.rb index 45984fdf0..779a9c853 100644 --- a/app/jobs/process_uploaded_file_job.rb +++ b/app/jobs/process_uploaded_file_job.rb @@ -88,15 +88,9 @@ def count_common_path_components(archive) end def count_common_elements(arrays) - common = [] - loop do - elements = arrays.filter_map(&:shift).uniq - if elements.count == 1 - common << elements[0] - else - break - end - end - common.count + return 0 if arrays.empty? + first = arrays.shift + zip = first.zip(*arrays) + zip.count { |x| x.uniq.count == 1 } end end diff --git a/spec/jobs/process_uploaded_file_job_spec.rb b/spec/jobs/process_uploaded_file_job_spec.rb index 310b86e52..1a0a20285 100644 --- a/spec/jobs/process_uploaded_file_job_spec.rb +++ b/spec/jobs/process_uploaded_file_job_spec.rb @@ -23,6 +23,20 @@ ])).to eq 2 end + it "returns correct count where all prefixes are the same" do + expect(job.send(:count_common_elements, [ + ["root", "sub", "folder"], + ["root", "sub", "folder"] + ])).to eq 3 + end + + it "returns correct count where a common folder has a subfolder" do + expect(job.send(:count_common_elements, [ + ["root", "sub"], + ["root", "sub", "folder2"] + ])).to eq 2 + end + it "returns zero for *some* common prefixes but not on everything" do expect(job.send(:count_common_elements, [ ["folder1", "sub1"], @@ -154,12 +168,12 @@ Zip::File.open(file, create: true) do |zipfile| zipfile.mkdir("sub") zipfile.mkdir("sub/folder") - zipfile.get_output_stream("sub/folder/test.stl") { |f| f.puts "solid" } + zipfile.get_output_stream("sub/test.stl") { |f| f.puts "solid" } zipfile.get_output_stream("sub/folder/test2.stl") { |f| f.puts "solid" } end described_class.new.send(:unzip, model, Rack::Test::UploadedFile.new(file)) expect(model.model_files.count).to eq 2 - expect(model.model_files.map(&:filename).sort).to eq ["test.stl", "test2.stl"] + expect(model.model_files.map(&:filename).sort).to eq ["folder/test2.stl", "test.stl"] end end