Skip to content

Commit ee2a7f6

Browse files
committed
fix: detect files matching the decoded or encoded id of an entity
1 parent ffe5b91 commit ee2a7f6

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/main/java/edu/kit/datamanager/ro_crate/reader/CrateReader.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Path;
2323
import java.util.*;
2424
import java.util.stream.Collectors;
25+
import java.util.stream.Stream;
2526
import java.util.stream.StreamSupport;
2627

2728
/**
@@ -142,6 +143,7 @@ private RoCrate rebuildCrate(ObjectNode metadataJson, File files, HashSet<String
142143

143144
// Handle data entities with corresponding file
144145
checkFolderHasFile(entityJson.get(PROP_ID).asText(), files).ifPresent(file -> {
146+
System.out.println("Found file for: " + file.toString());
145147
usedFiles.add(file.getPath());
146148
builder.setLocationWithExceptions(file.toPath())
147149
.setId(file.getName());
@@ -237,15 +239,24 @@ protected String unpackId(JsonNode node) {
237239
}
238240

239241
protected Optional<File> checkFolderHasFile(String filepathOrId, File folder) {
242+
System.out.println("DEBUG: " + Arrays.toString(folder.listFiles()));
240243
if (IdentifierUtils.isUrl(filepathOrId)) {
241244
return Optional.empty();
242245
}
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())
245255
// defence-in-depth: ensure we are still inside the crate folder
246256
.filter(resolved -> resolved.startsWith(folder.toPath()))
247257
.map(Path::toFile)
248-
.filter(File::exists);
258+
.filter(File::exists)
259+
.findFirst();
249260
}
250261

251262
/**

0 commit comments

Comments
 (0)