From 4d251e0732c353eb28516326f909a8bc362d4c1d Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 2 Apr 2019 18:14:09 +0200 Subject: Fixed file listing in subdirectories --- src/main/kotlin/App.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 2da5ced..993ac9d 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -1,6 +1,7 @@ package space.anity import io.javalin.Javalin +import io.javalin.NotFoundResponse import java.io.File import java.nio.file.Files import java.nio.file.Paths @@ -15,12 +16,18 @@ fun main(args: Array) { app.get("/files/*") { ctx -> var files = "" - Files.list(Paths.get("$fileHome${ctx.splats()[0]}/")).forEach { - val fileName = it.toString().drop(fileHome.length + (if (ctx.splats()[0].isNotEmpty()) ctx.splats()[0].length + 1 else 0)) - val filePath = "$fileHome${it.toString().drop(fileHome.length)}" - files += if (File("$fileHome${it.toString().drop(fileHome.length)}").isDirectory) "$fileName/\n" else "$fileName\n" + try { + Files.list(Paths.get("$fileHome/${ctx.splats()[0]}/")).forEach { + val fileName = it.toString() + .drop(fileHome.length + (if (ctx.splats()[0].isNotEmpty()) ctx.splats()[0].length + 1 else 0)) + val filePath = "$fileHome${it.toString().drop(fileHome.length)}" + files += if (File(filePath).isDirectory) "$fileName/\n" else "$fileName\n" + } + ctx.result(files) + } catch (_: java.nio.file.NoSuchFileException) { + throw NotFoundResponse("File or directory does not exist") } - ctx.result(files) + //File("test").writeText(ctx.splat(0)!!) } } -- cgit v1.2.3