aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-04-02 18:14:09 +0200
committerMarvin Borner2019-04-02 18:14:09 +0200
commit4d251e0732c353eb28516326f909a8bc362d4c1d (patch)
treec31c7ef25b98a73cb9d96a5f99206212c96c98e7
parent0742b3437ec57b13b11b606d0657f5e886e58d94 (diff)
Fixed file listing in subdirectories
-rw-r--r--src/main/kotlin/App.kt17
1 files changed, 12 insertions, 5 deletions
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<String>) {
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)!!)
}
}