From 71eb415c73ef768e665021019cc1f0f138f1fba0 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Thu, 12 Sep 2024 14:45:03 +0100 Subject: [PATCH] Use BFS to detect `ro-crate-metadata.json`. Fixes #32 --- lib/ro_crate/reader.rb | 6 +++++- test/fixtures/multi_metadata_crate.crate.zip | Bin 0 -> 903 bytes test/reader_test.rb | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/multi_metadata_crate.crate.zip diff --git a/lib/ro_crate/reader.rb b/lib/ro_crate/reader.rb index a26e682..0eb5ba0 100644 --- a/lib/ro_crate/reader.rb +++ b/lib/ro_crate/reader.rb @@ -306,12 +306,16 @@ def self.extract_root_entity(entities) # @param source [String, ::File, Pathname] The location of the directory. # @return [Pathname, nil] The path to the root, or nil if not found. def self.detect_root_directory(source) - Pathname(source).find do |entry| + queue = [source] + until queue.empty? + entry = Pathname(queue.shift) if entry.file? name = entry.basename.to_s if name == ROCrate::Metadata::IDENTIFIER || name == ROCrate::Metadata::IDENTIFIER_1_0 return entry.parent end + elsif entry.directory? + queue += entry.children end end diff --git a/test/fixtures/multi_metadata_crate.crate.zip b/test/fixtures/multi_metadata_crate.crate.zip new file mode 100644 index 0000000000000000000000000000000000000000..c2084e25940102dd8c87731a5efc8b3d6151e493 GIT binary patch literal 903 zcmWIWW@Zs#W&nZ%MLLln8U}=bjKsYBl8n@%_>|0|)Z~)_b3x z9gGYN8d%jA=BAb;rX-dm>SYz@=e73R^EVj?9Q|I`wQTbR0dqCs*gG;l zG8~6FMK2tf`1)tNz`orMOJq$pI=7d<|NAahe@Vz}9%(cEu#F+A#o5Vg=fBqCpUCCA z!kEd-ic5K~M0oKUg^bgNQ#dU5)%{c7drDm>(SW`Amcf$)0&%)#SxIEyy zW?61*O)JZfC`wgP+$41RkPpuz-T-e#CVOUFiA4qIZ4eM>`0EIwF_R7#g91o`fssLi zpelgbDj16y>+y>K7KEe)c2@Gb3Lc^9u1E5Kmc3=;9tTwannTXG3)F?%^*#a2J b#M{G=YWxV*9#%F`C@=$|8_*