diff options
Diffstat (limited to 'src/main/kotlin/App.kt')
-rw-r--r-- | src/main/kotlin/App.kt | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 79c4e83..d7467a4 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -6,35 +6,34 @@ import java.io.* import java.nio.file.* fun main(args: Array<String>) { - val app = Javalin.create().enableStaticFiles("../resources/").start(7000) val fileHome = "files" + val app = Javalin.create().enableStaticFiles("../resources/").start(7000) - app.get("/") { ctx -> - ctx.result("Hello World") - } - + // TODO: Fix possible security issue with "../" app.get("/files/*") { ctx -> - var files = "" + val files = ArrayList<String>() 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)) + .drop(fileHome.length + (if (ctx.splats()[0].isNotEmpty()) ctx.splats()[0].length + 2 else 1)) val filePath = "$fileHome${it.toString().drop(fileHome.length)}" - files += if (File(filePath).isDirectory) "$fileName/\n" else "$fileName\n" + files.add(if (File(filePath).isDirectory) "$fileName/" else fileName) } - ctx.result(files) + ctx.json(files) } catch (_: java.nio.file.NoSuchFileException) { throw NotFoundResponse("Error: File or directory does not exist.") } - - //File("test").writeText(ctx.splat(0)!!) } + app.get("/upload") { ctx -> ctx.redirect("/upload.html") } + + // TODO: Fix possible security issue with "../" app.post("/upload") { ctx -> ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) -> - if (ctx.queryParam("dir") !== null) + if (ctx.queryParam("dir") !== null) { FileUtil.streamToFile(content, "files/${ctx.queryParam("dir")}/$name") - else + ctx.redirect("/upload.html") + } else throw BadRequestResponse("Error: Please enter a filename.") } } |