|
22 | 22 | import java.nio.file.Path; |
23 | 23 | import java.util.*; |
24 | 24 | import java.util.stream.Collectors; |
| 25 | +import java.util.stream.Stream; |
25 | 26 | import java.util.stream.StreamSupport; |
26 | 27 |
|
27 | 28 | /** |
@@ -142,6 +143,7 @@ private RoCrate rebuildCrate(ObjectNode metadataJson, File files, HashSet<String |
142 | 143 |
|
143 | 144 | // Handle data entities with corresponding file |
144 | 145 | checkFolderHasFile(entityJson.get(PROP_ID).asText(), files).ifPresent(file -> { |
| 146 | + System.out.println("Found file for: " + file.toString()); |
145 | 147 | usedFiles.add(file.getPath()); |
146 | 148 | builder.setLocationWithExceptions(file.toPath()) |
147 | 149 | .setId(file.getName()); |
@@ -237,15 +239,24 @@ protected String unpackId(JsonNode node) { |
237 | 239 | } |
238 | 240 |
|
239 | 241 | protected Optional<File> checkFolderHasFile(String filepathOrId, File folder) { |
| 242 | + System.out.println("DEBUG: " + Arrays.toString(folder.listFiles())); |
240 | 243 | if (IdentifierUtils.isUrl(filepathOrId)) { |
241 | 244 | return Optional.empty(); |
242 | 245 | } |
243 | | - return IdentifierUtils.decode(filepathOrId) |
244 | | - .map(decoded -> folder.toPath().resolve(decoded).normalize()) |
| 246 | + System.out.println( "DEBUG: " + |
| 247 | + IdentifierUtils.decode(filepathOrId) |
| 248 | + .map(decoded -> folder.toPath().resolve(decoded).normalize()) |
| 249 | + .filter(resolved -> resolved.startsWith(folder.toPath())) |
| 250 | + .map(Path::toFile) |
| 251 | + .map(File::exists) |
| 252 | + ); |
| 253 | + return Stream.of(IdentifierUtils.decode(filepathOrId).orElse(filepathOrId), filepathOrId) |
| 254 | + .map(filename -> folder.toPath().resolve(filename).normalize().toAbsolutePath()) |
245 | 255 | // defence-in-depth: ensure we are still inside the crate folder |
246 | 256 | .filter(resolved -> resolved.startsWith(folder.toPath())) |
247 | 257 | .map(Path::toFile) |
248 | | - .filter(File::exists); |
| 258 | + .filter(File::exists) |
| 259 | + .findFirst(); |
249 | 260 | } |
250 | 261 |
|
251 | 262 | /** |
|
0 commit comments