Skip to content

Commit

Permalink
Merge pull request #33 from ResearchObject/skip-preview-generation
Browse files Browse the repository at this point in the history
Add an option to writer to avoid generating the ro-crate-preview
  • Loading branch information
fbacall authored Sep 13, 2024
2 parents 21ae238 + 2427475 commit df04950
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/ro_crate/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit df04950

Please sign in to comment.