Skip to content

Commit df04950

Browse files
authored
Merge pull request #33 from ResearchObject/skip-preview-generation
Add an option to writer to avoid generating the ro-crate-preview
2 parents 21ae238 + 2427475 commit df04950

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/ro_crate/writer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def initialize(crate)
1414
#
1515
# @param dir [String] A path for the directory for the crate to be written to. All parent directories will be created.
1616
# @param overwrite [Boolean] Whether or not to overwrite existing files.
17-
def write(dir, overwrite: true)
17+
# @param skip_preview [Boolean] Whether or not to skip generation of the RO-Crate preview HTML file.
18+
def write(dir, overwrite: true, skip_preview: false)
1819
FileUtils.mkdir_p(dir) # Make any parent directories
1920
@crate.payload.each do |path, entry|
21+
next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator)
2022
fullpath = ::File.join(dir, path)
2123
next if !overwrite && ::File.exist?(fullpath)
2224
next if entry.directory?
@@ -40,10 +42,12 @@ def write(dir, overwrite: true)
4042
# Write the crate to a zip file.
4143
#
4244
# @param destination [String, ::File] The destination where to write the RO-Crate zip.
43-
def write_zip(destination)
45+
# @param skip_preview [Boolean] Whether or not to skip generation of the RO-Crate preview HTML file.
46+
def write_zip(destination, skip_preview: false)
4447
Zip::File.open(destination, Zip::File::CREATE) do |zip|
4548
@crate.payload.each do |path, entry|
4649
next if entry.directory?
50+
next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator)
4751
if entry.symlink?
4852
zip.add(path, entry.path) if entry.path
4953
else

test/writer_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,39 @@ class WriterTest < Test::Unit::TestCase
274274
end
275275
end
276276
end
277+
278+
test 'skip generating the preview in a directory' do
279+
crate = ROCrate::Crate.new
280+
crate.add_file(fixture_file('info.txt'))
281+
crate.add_file(StringIO.new('just a string!'), 'notice.txt')
282+
crate.add_file(fixture_file('data.csv'), 'directory/data.csv')
283+
284+
Dir.mktmpdir do |dir|
285+
ROCrate::Writer.new(crate).write(dir, skip_preview: true)
286+
assert ::File.exist?(::File.join(dir, ROCrate::Metadata::IDENTIFIER))
287+
refute ::File.exist?(::File.join(dir, ROCrate::Preview::IDENTIFIER))
288+
assert_equal 6, ::File.size(::File.join(dir, 'info.txt'))
289+
assert_equal 14, ::File.size(::File.join(dir, 'notice.txt'))
290+
assert_equal 20, ::File.size(::File.join(dir, 'directory', 'data.csv'))
291+
end
292+
end
293+
294+
test 'skip generating the preview in a zip file' do
295+
crate = ROCrate::Crate.new
296+
crate.add_directory(fixture_file('directory').path.to_s, 'fish')
297+
298+
Tempfile.create do |file|
299+
ROCrate::Writer.new(crate).write_zip(file, skip_preview: true)
300+
301+
Zip::File.open(file) do |zipfile|
302+
assert zipfile.file.exist?(ROCrate::Metadata::IDENTIFIER)
303+
refute zipfile.file.exist?(ROCrate::Preview::IDENTIFIER)
304+
assert zipfile.file.exist? 'fish/info.txt'
305+
assert zipfile.file.exist? 'fish/root.txt'
306+
assert zipfile.file.exist? 'fish/data/info.txt'
307+
assert zipfile.file.exist? 'fish/data/nested.txt'
308+
assert zipfile.file.exist? 'fish/data/binary.jpg'
309+
end
310+
end
311+
end
277312
end

0 commit comments

Comments
 (0)