diff --git a/lib/ro_crate/writer.rb b/lib/ro_crate/writer.rb index 619680a..e53c829 100644 --- a/lib/ro_crate/writer.rb +++ b/lib/ro_crate/writer.rb @@ -14,9 +14,11 @@ def initialize(crate) # # @param dir [String] A path for the directory for the crate to be written to. All parent directories will be created. # @param overwrite [Boolean] Whether or not to overwrite existing files. - def write(dir, overwrite: true) + # @param skip_preview [Boolean] Whether or not to skip generation of the RO-Crate preview HTML file. + def write(dir, overwrite: true, skip_preview: false) FileUtils.mkdir_p(dir) # Make any parent directories @crate.payload.each do |path, entry| + next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator) fullpath = ::File.join(dir, path) next if !overwrite && ::File.exist?(fullpath) next if entry.directory? @@ -40,10 +42,12 @@ def write(dir, overwrite: true) # Write the crate to a zip file. # # @param destination [String, ::File] The destination where to write the RO-Crate zip. - def write_zip(destination) + # @param skip_preview [Boolean] Whether or not to skip generation of the RO-Crate preview HTML file. + def write_zip(destination, skip_preview: false) Zip::File.open(destination, Zip::File::CREATE) do |zip| @crate.payload.each do |path, entry| next if entry.directory? + next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator) if entry.symlink? zip.add(path, entry.path) if entry.path else diff --git a/test/writer_test.rb b/test/writer_test.rb index 255ea3a..340e87b 100644 --- a/test/writer_test.rb +++ b/test/writer_test.rb @@ -274,4 +274,39 @@ class WriterTest < Test::Unit::TestCase end end end + + test 'skip generating the preview in a directory' do + crate = ROCrate::Crate.new + crate.add_file(fixture_file('info.txt')) + crate.add_file(StringIO.new('just a string!'), 'notice.txt') + crate.add_file(fixture_file('data.csv'), 'directory/data.csv') + + Dir.mktmpdir do |dir| + ROCrate::Writer.new(crate).write(dir, skip_preview: true) + assert ::File.exist?(::File.join(dir, ROCrate::Metadata::IDENTIFIER)) + refute ::File.exist?(::File.join(dir, ROCrate::Preview::IDENTIFIER)) + assert_equal 6, ::File.size(::File.join(dir, 'info.txt')) + assert_equal 14, ::File.size(::File.join(dir, 'notice.txt')) + assert_equal 20, ::File.size(::File.join(dir, 'directory', 'data.csv')) + end + end + + test 'skip generating the preview in a zip file' do + crate = ROCrate::Crate.new + crate.add_directory(fixture_file('directory').path.to_s, 'fish') + + Tempfile.create do |file| + ROCrate::Writer.new(crate).write_zip(file, skip_preview: true) + + Zip::File.open(file) do |zipfile| + assert zipfile.file.exist?(ROCrate::Metadata::IDENTIFIER) + refute zipfile.file.exist?(ROCrate::Preview::IDENTIFIER) + assert zipfile.file.exist? 'fish/info.txt' + assert zipfile.file.exist? 'fish/root.txt' + assert zipfile.file.exist? 'fish/data/info.txt' + assert zipfile.file.exist? 'fish/data/nested.txt' + assert zipfile.file.exist? 'fish/data/binary.jpg' + end + end + end end