Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed another edge case in path stripping #2863

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions app/jobs/process_uploaded_file_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 16 additions & 2 deletions spec/jobs/process_uploaded_file_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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

Expand Down
Loading