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

Add an option to writer to avoid generating the ro-crate-preview #33

Merged
merged 1 commit into from
Sep 13, 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
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
Loading