Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit d55768f

Browse files
authored
Merge pull request #85 from Ktt-Development/fix-null-dir-ref@ec30cf9
Fixed #84: Directories return null file source for FileHandler
2 parents ec30cf9 + 94a5939 commit d55768f

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kttdevelopment</groupId>
88
<artifactId>simplehttpserver</artifactId>
9-
<version>03.05.05</version>
9+
<version>03.05.06</version>
1010
<packaging>jar</packaging>
1111

1212
<url>https://github.com/Ktt-Development/simplehttpserver</url>

src/main/java/com/kttdevelopment/simplehttpserver/handler/DirectoryEntry.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see FileHandler
1919
* @see FileEntry
2020
* @since 02.00.00
21-
* @version 03.05.04
21+
* @version 03.05.06
2222
* @author Ktt Development
2323
*/
2424
class DirectoryEntry {
@@ -248,12 +248,15 @@ public final File getFile(final String path){
248248
// if not top level directory or if not child of directory folder, then return null file
249249
if(!pabs.equals(dabs) && (!isWalkthrough || !pabs.startsWith(dabs))) return null;
250250

251-
final String fileName = new File(dabs + relative).getName();
251+
final File targetFile = Paths.get(dabs,relative).toFile();
252+
final String fileName = targetFile.getParentFile() == null ? targetFile.getPath() : targetFile.getName();
252253

253254
// for each file in parent directory, run adapter to find file that matches adapted name
254255
for(final File file : Objects.requireNonNullElse(parentFile.listFiles(), new File[0]))
255256
if(!file.isDirectory() && adapter.getName(file).equals(fileName))
256257
return file;
258+
else if(file.isDirectory() && file.getName().equals(fileName)) // directories are not subject to adapter names
259+
return file;
257260
return null;
258261
}
259262
}
@@ -275,7 +278,7 @@ public final byte[] getBytes(final String path){
275278
}else{
276279
try{
277280
final File file = Objects.requireNonNull(getFile(path)); // find if file allowed
278-
return adapter.getBytes(file,Files.readAllBytes(file.toPath())); // adapt bytes here
281+
return !file.isDirectory() ? adapter.getBytes(file,Files.readAllBytes(file.toPath())) : null; // adapt bytes here
279282
}catch(final NullPointerException | IOException ignored){
280283
return null;
281284
}

0 commit comments

Comments
 (0)