diff options
author | Marvin Borner | 2019-04-02 20:21:59 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-02 20:21:59 +0200 |
commit | 19c341e4642149b066667f042656a4f045293efd (patch) | |
tree | 438a3411973f35edac410aa01637b8d27ebfc12e | |
parent | ab5c6108c9c0404fb0b971e8145078ac8ce6a2ad (diff) |
Added basic upload functionality
-rw-r--r-- | src/main/kotlin/App.kt | 22 | ||||
-rw-r--r-- | src/main/resources/upload.html | 16 |
2 files changed, 31 insertions, 7 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 993ac9d..79c4e83 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -1,13 +1,12 @@ 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 +import io.javalin.* +import io.javalin.core.util.* +import java.io.* +import java.nio.file.* fun main(args: Array<String>) { - val app = Javalin.create().start(7000) + val app = Javalin.create().enableStaticFiles("../resources/").start(7000) val fileHome = "files" app.get("/") { ctx -> @@ -25,9 +24,18 @@ fun main(args: Array<String>) { } ctx.result(files) } catch (_: java.nio.file.NoSuchFileException) { - throw NotFoundResponse("File or directory does not exist") + throw NotFoundResponse("Error: File or directory does not exist.") } //File("test").writeText(ctx.splat(0)!!) } + + app.post("/upload") { ctx -> + ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) -> + if (ctx.queryParam("dir") !== null) + FileUtil.streamToFile(content, "files/${ctx.queryParam("dir")}/$name") + else + throw BadRequestResponse("Error: Please enter a filename.") + } + } } diff --git a/src/main/resources/upload.html b/src/main/resources/upload.html new file mode 100644 index 0000000..550f841 --- /dev/null +++ b/src/main/resources/upload.html @@ -0,0 +1,16 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" + name="viewport"> + <meta content="ie=edge" http-equiv="X-UA-Compatible"> + <title>Upload Testing</title> +</head> +<body> +<form action="/upload?dir=test" enctype="multipart/form-data" method="post"> + <input multiple name="files" type="file"> + <button>Submit</button> +</form> +</body> +</html> |