Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public void addAuthorId(String id) {
this.addIdProperty("author", id);
}

/**
* Gets the path of this entity.
* <p>
* When reading a crate, this will point to the entity's file within the crate.
* When creating a new entity or crate, it might point to a file outside the crate,
* as set by {@link DataEntityBuilder#setLocation(Path)}.
* Such a file will be copied into the crate when writing.
*
* @return the path to the file this entity represents.
*/
@JsonIgnore
public Path getPath() {
return path;
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/edu/kit/datamanager/ro_crate/reader/CrateReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

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

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

protected Optional<File> checkFolderHasFile(String filepathOrId, File folder) {
System.out.println("DEBUG: " + Arrays.toString(folder.listFiles()));
if (IdentifierUtils.isUrl(filepathOrId)) {
return Optional.empty();
}
return IdentifierUtils.decode(filepathOrId)
.map(decoded -> folder.toPath().resolve(decoded).normalize())
System.out.println( "DEBUG: " +
IdentifierUtils.decode(filepathOrId)
.map(decoded -> folder.toPath().resolve(decoded).normalize())
.filter(resolved -> resolved.startsWith(folder.toPath()))
.map(Path::toFile)
.map(File::exists)
);
return Stream.of(IdentifierUtils.decode(filepathOrId).orElse(filepathOrId), filepathOrId)
.map(filename -> folder.toPath().resolve(filename).normalize().toAbsolutePath())
// defence-in-depth: ensure we are still inside the crate folder
.filter(resolved -> resolved.startsWith(folder.toPath()))
.map(Path::toFile)
.filter(File::exists);
.filter(File::exists)
.findFirst();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.kit.datamanager.ro_crate.Crate;
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
import edu.kit.datamanager.ro_crate.special.IdentifierUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -64,16 +65,18 @@ public void save(Crate crate, String destination) throws IOException {
}
}
for (DataEntity dataEntity : crate.getAllDataEntities()) {
savetoFile(dataEntity, file);
saveToFile(dataEntity, file);
}
}

private void savetoFile(DataEntity entity, File file) throws IOException {
private void saveToFile(DataEntity entity, File file) throws IOException {
if (entity.getPath() != null) {
String id = entity.getId();
String filename = IdentifierUtils.decode(id).orElse(id);
if (entity.getPath().toFile().isDirectory()) {
FileUtils.copyDirectory(entity.getPath().toFile(), file.toPath().resolve(entity.getId()).toFile());
FileUtils.copyDirectory(entity.getPath().toFile(), file.toPath().resolve(filename).toFile());
} else {
FileUtils.copyFile(entity.getPath().toFile(), file.toPath().resolve(entity.getId()).toFile());
FileUtils.copyFile(entity.getPath().toFile(), file.toPath().resolve(filename).toFile());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.UUID;

import edu.kit.datamanager.ro_crate.preview.CratePreview;
import edu.kit.datamanager.ro_crate.special.IdentifierUtils;
import edu.kit.datamanager.ro_crate.util.FileSystemUtil;
import edu.kit.datamanager.ro_crate.util.ZipStreamUtil;
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
Expand Down Expand Up @@ -154,16 +155,18 @@ private void saveToStream(DataEntity entity, ZipOutputStream zipStream, String p
}

boolean isDirectory = entity.getPath().toFile().isDirectory();
String id = entity.getId();
String filename = IdentifierUtils.decode(id).orElse(id);
if (isDirectory) {
ZipStreamUtil.addFolderToZipStream(
zipStream,
entity.getPath().toFile(),
prefix + entity.getId());
prefix + filename);
} else {
ZipStreamUtil.addFileToZipStream(
zipStream,
entity.getPath().toFile(),
prefix + entity.getId());
prefix + filename);
}
}
}
Loading
Loading