Skip to content

Commit

Permalink
Added depth to parent folder name generator
Browse files Browse the repository at this point in the history
depth == 0  is the parent folder,
  • Loading branch information
net-cscience-raphael committed Mar 5, 2024
1 parent 043acd3 commit 850f4f0
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ public class ParentFolderNameObjectIdGenerator implements ObjectIdGenerator {
* Prefix property name. Can be set to false to prevent type prefix in object ID.
*/
private static final String PROPERTY_NAME_PREFIX = "prefix";
private static final String PROPERTY_NAME_DEPTH = "depth";

private final boolean prefix;
private final int depth;

public ParentFolderNameObjectIdGenerator() {
prefix = true;
depth = 0;
}

public ParentFolderNameObjectIdGenerator(Map<String, String> properties) {
String prefixProp = properties.get(PROPERTY_NAME_PREFIX);
prefix = prefixProp == null || prefixProp.equalsIgnoreCase("true");
this.prefix = prefixProp == null || prefixProp.equalsIgnoreCase("true");

String depthProp = properties.get(PROPERTY_NAME_DEPTH);
this.depth = depthProp == null ? 0 : Integer.parseInt(depthProp);
assert depth >= 0 && depth < 10;
}

@Override
Expand All @@ -40,8 +47,15 @@ public Optional<String> next(Path path, MediaType type) {
return Optional.empty();
}

String filename = FilenameUtils.getBaseName(path.toFile().getParentFile().getName());
String filename = FilenameUtils.getBaseName(getFolderName(path, this.depth));
String id = prefix ? MediaType.generateId(type, filename) : filename;
return Optional.of(id);
}

private String getFolderName(Path path, int depth) {
if (depth == 0) {
return path.toFile().getParentFile().getName();
}
return getFolderName(path.getParent(), depth - 1);
}
}

0 comments on commit 850f4f0

Please sign in to comment.