Skip to content

Commit

Permalink
fix: import collection export zip when directory entries present (#1841)
Browse files Browse the repository at this point in the history
* test: test import with modified zip (missing schema.json, rezipped)

* fix: skip directory entries when parsing import collection zip
  • Loading branch information
PiTrem authored Mar 28, 2024
1 parent cca1122 commit 3c2acc1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/import/import_collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ def extract
att = Tempfile.new(encoding: 'ascii-8bit')
att.write(@att.read_file)
att.rewind

Zip::File.open(att.path) do |zip_file|
# Handle entries one by one
zip_file.each do |entry|
# do nothing for directory entry
next if entry.ftype == :directory

data = entry.get_input_stream.read.force_encoding('UTF-8')
case entry.name
when 'export.json'
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
file_path { Rails.root.join('spec/fixtures/import/collection_samples.zip') }
end

# copy of the collection_samples.zip file but with removed schema.json file
# and reziped (zip contains directory entries)
trait :with_edited_collection_zip do
file_path { Rails.root.join('spec/fixtures/import/collection_samples_edited.zip') }
end

trait :with_reaction_collection_zip do
file_path { Rails.root.join('spec/fixtures/import/collection_reaction.zip') }
end
Expand Down
Binary file not shown.
5 changes: 3 additions & 2 deletions spec/lib/import/import_collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

let(:importer) { Import::ImportCollections.new(attachment, user.id) }

describe 'import a collection with samples' do
describe 'import a collection with samples, with directories in the zip structure and missing schema.json' do
let(:import_id) { 'collection_samples' }
let(:attachment) { create(:attachment, :with_sample_collection_zip) }
# let(:attachment) { create(:attachment, :with_sample_collection_zip) }
let(:attachment) { create(:attachment, :with_edited_collection_zip) }

it 'successfully import 2 samples' do # rubocop:disable RSpec/MultipleExpectations
importer.execute
Expand Down

0 comments on commit 3c2acc1

Please sign in to comment.