Skip to content

Commit 2d99dbb

Browse files
authored
fix: potentially address subtle bug with file paths getting a trailing slash (#161)
1 parent 8d18609 commit 2d99dbb

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

gestalt-module/src/main/java/org/terasology/gestalt/module/resources/ClasspathFileSource.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Optional<FileReference> getFile(List<String> filepath) {
102102
if (filepath.stream().anyMatch(s -> s.equals(".."))) {
103103
return Optional.empty();
104104
}
105-
String fullpath = buildPathString(filepath);
105+
String fullpath = buildPathString(filepath, false);
106106
if (classLoader.getResource(fullpath) != null) {
107107
return Optional.of(new ClasspathSourceFileReference(fullpath, extractSubpath(basePath, fullpath), classLoader));
108108
} else {
@@ -112,7 +112,7 @@ public Optional<FileReference> getFile(List<String> filepath) {
112112

113113
@Override
114114
public Collection<FileReference> getFilesInPath(boolean recursive, List<String> path) {
115-
String fullPath = buildPathString(path);
115+
String fullPath = buildPathString(path, true);
116116
Stream<String> candidates = files
117117
.stream()
118118
.filter(file -> file.startsWith(fullPath))
@@ -129,7 +129,7 @@ public Collection<FileReference> getFilesInPath(boolean recursive, List<String>
129129

130130
@Override
131131
public Set<String> getSubpaths(List<String> path) {
132-
String fullPath = buildPathString(path);
132+
String fullPath = buildPathString(path, true);
133133
return files
134134
.stream()
135135
.filter(file -> file.startsWith(fullPath))
@@ -141,12 +141,15 @@ public Set<String> getSubpaths(List<String> path) {
141141
.collect(Collectors.toSet());
142142
}
143143

144-
private String buildPathString(List<String> path) {
144+
private String buildPathString(List<String> path, boolean isDirectory) {
145145
String fullPath;
146146
if (path.isEmpty() || (path.size() == 1 && path.get(0).isEmpty())) {
147147
fullPath = basePath;
148148
} else {
149-
fullPath = basePath + CLASS_PATH_JOINER.join(path) + CLASS_PATH_SEPARATOR;
149+
fullPath = basePath + CLASS_PATH_JOINER.join(path);
150+
if (isDirectory) {
151+
fullPath += CLASS_PATH_SEPARATOR;
152+
}
150153
}
151154
return fullPath;
152155
}

0 commit comments

Comments
 (0)