diff options
-rw-r--r-- | src/main/kotlin/App.kt | 86 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt (renamed from src/main/kotlin/DB.kt) | 11 | ||||
-rw-r--r-- | src/main/resources/compiled-views/files.java | 35 | ||||
-rw-r--r-- | src/main/resources/compiled-views/fileview.java | 34 |
4 files changed, 98 insertions, 68 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 4682e92..a9e2018 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -8,52 +8,24 @@ import io.javalin.rendering.template.TemplateUtil.model import java.io.* import java.nio.file.* - +const val fileHome = "files" +val db = DatabaseController() fun main() { val app = Javalin.create().enableStaticFiles("../resources/").start(7000) - val fileHome = "files" JavalinRenderer.register( FileRenderer { filepath, model -> Rocker.template(filepath).bind(model).render().toString() }, ".rocker.html" ) - val db = DB() - - // TODO: If not initialUse show setup page - /** * Sends a json object of filenames in [fileHome]s * TODO: Fix possible security issue with "../" */ - app.get("/files/*") { ctx -> - val files = ArrayList<String>() - try { - if (File("$fileHome/${ctx.splats()[0]}").isDirectory) { - 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 + 2 else 1)) - val filePath = "$fileHome${it.toString().drop(fileHome.length)}" - files.add(if (File(filePath).isDirectory) "$fileName/" else fileName) - ctx.render("files.rocker.html", model("files", files)) - } - } else - // TODO: Fix square brackets at fileview content - ctx.render( - "fileview.rocker.html", model( - "content", Files.readAllLines( - Paths.get("$fileHome/${ctx.splats()[0]}"), - Charsets.UTF_8 - ).toString() - ) - ) - } catch (_: java.nio.file.NoSuchFileException) { - throw NotFoundResponse("Error: File or directory does not exist.") - } - } + app.get("/files/*") { ctx -> crawlFiles(ctx) } /** - * Redirects to corresponding html file + * Redirects upload to corresponding html file */ app.get("/upload") { ctx -> ctx.redirect("/views/upload.html") } @@ -61,14 +33,48 @@ fun main() { * Receives and saves multipart media data * TODO: Fix possible security issue with "../" */ - 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") - ctx.redirect("/views/upload.html") - } else - throw BadRequestResponse("Error: Please enter a filename.") - } + app.post("/upload") { ctx -> upload(ctx) } +} + +/** + * Crawls the requested directory and returns filenames in array + */ +fun crawlFiles(ctx: Context) { + val files = ArrayList<String>() + try { + if (File("$fileHome/${ctx.splats()[0]}").isDirectory) { + 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 + 2 else 1)) + val filePath = "$fileHome${it.toString().drop(fileHome.length)}" + files.add(if (File(filePath).isDirectory) "$fileName/" else fileName) + ctx.render("files.rocker.html", model("files", files)) + } + } else + // TODO: Fix square brackets at fileview content + ctx.render( + "fileview.rocker.html", model( + "content", Files.readAllLines( + Paths.get("$fileHome/${ctx.splats()[0]}"), + Charsets.UTF_8 + ).toString() + ) + ) + } catch (_: java.nio.file.NoSuchFileException) { + throw NotFoundResponse("Error: File or directory does not exist.") + } +} + +/** + * Saves multipart media data into requested directory + */ +fun upload(ctx: Context) { + ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) -> + if (ctx.queryParam("dir") !== null) { + FileUtil.streamToFile(content, "files/${ctx.queryParam("dir")}/$name") + ctx.redirect("/views/upload.html") + } else + throw BadRequestResponse("Error: Please enter a filename.") } } diff --git a/src/main/kotlin/DB.kt b/src/main/kotlin/DatabaseController.kt index a01a1e6..423dd05 100644 --- a/src/main/kotlin/DB.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -4,9 +4,8 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.* import java.sql.* - -class DB(val dbFileLocation: String = "main.db") { - val db :Database +class DatabaseController(dbFileLocation: String = "main.db") { + val db: Database /** * Database table for the file location indexing @@ -30,12 +29,12 @@ class DB(val dbFileLocation: String = "main.db") { * Database table storing general data/states */ object General : Table() { - val initialUse = integer("initialUse").default(0).primaryKey() // boolean -> 0:1 + val initialUse = integer("initialUse").default(1).primaryKey() // boolean -> 0:1 } init { // create connection - this.db = Database.connect("jdbc:sqlite:main.db", "org.sqlite.JDBC") + this.db = Database.connect("jdbc:sqlite:$dbFileLocation", "org.sqlite.JDBC") TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // add tables @@ -45,4 +44,4 @@ class DB(val dbFileLocation: String = "main.db") { } // TODO add functions for database usage -}
\ No newline at end of file +} diff --git a/src/main/resources/compiled-views/files.java b/src/main/resources/compiled-views/files.java index ebd0c58..b37859f 100644 --- a/src/main/resources/compiled-views/files.java +++ b/src/main/resources/compiled-views/files.java @@ -1,14 +1,13 @@ -import java.io.IOException; -import com.fizzed.rocker.ForIterator; import com.fizzed.rocker.RenderingException; -import com.fizzed.rocker.RockerContent; -import com.fizzed.rocker.RockerOutput; import com.fizzed.rocker.runtime.DefaultRockerTemplate; import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader; -// import @ [1:1] + +import java.io.IOException; import java.util.ArrayList; +// import @ [1:1] + /* * Auto generated code to render template /files.rocker.html * Do not edit this file. Changes will eventually be overwritten by Rocker parser! @@ -16,11 +15,25 @@ import java.util.ArrayList; @SuppressWarnings("unused") public class files extends com.fizzed.rocker.runtime.DefaultRockerModel { - static public com.fizzed.rocker.ContentType getContentType() { return com.fizzed.rocker.ContentType.HTML; } - static public String getTemplateName() { return "files.rocker.html"; } - static public String getTemplatePackageName() { return ""; } - static public String getHeaderHash() { return "-1618097059"; } - static public String[] getArgumentNames() { return new String[] { "files" }; } + static public com.fizzed.rocker.ContentType getContentType() { + return com.fizzed.rocker.ContentType.HTML; + } + + static public String getTemplateName() { + return "files.rocker.html"; + } + + static public String getTemplatePackageName() { + return ""; + } + + static public String getHeaderHash() { + return "-1618097059"; + } + + static public String[] getArgumentNames() { + return new String[]{"files"}; + } // argument @ [2:2] private ArrayList files; @@ -36,7 +49,7 @@ public class files extends com.fizzed.rocker.runtime.DefaultRockerModel { static public files template(ArrayList files) { return new files() - .files(files); + .files(files); } @Override diff --git a/src/main/resources/compiled-views/fileview.java b/src/main/resources/compiled-views/fileview.java index e01b7f3..67de59f 100644 --- a/src/main/resources/compiled-views/fileview.java +++ b/src/main/resources/compiled-views/fileview.java @@ -1,12 +1,10 @@ -import java.io.IOException; -import com.fizzed.rocker.ForIterator; import com.fizzed.rocker.RenderingException; -import com.fizzed.rocker.RockerContent; -import com.fizzed.rocker.RockerOutput; import com.fizzed.rocker.runtime.DefaultRockerTemplate; import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader; +import java.io.IOException; + /* * Auto generated code to render template /fileview.rocker.html * Do not edit this file. Changes will eventually be overwritten by Rocker parser! @@ -14,11 +12,25 @@ import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader; @SuppressWarnings("unused") public class fileview extends com.fizzed.rocker.runtime.DefaultRockerModel { - static public com.fizzed.rocker.ContentType getContentType() { return com.fizzed.rocker.ContentType.HTML; } - static public String getTemplateName() { return "fileview.rocker.html"; } - static public String getTemplatePackageName() { return ""; } - static public String getHeaderHash() { return "868254209"; } - static public String[] getArgumentNames() { return new String[] { "content" }; } + static public com.fizzed.rocker.ContentType getContentType() { + return com.fizzed.rocker.ContentType.HTML; + } + + static public String getTemplateName() { + return "fileview.rocker.html"; + } + + static public String getTemplatePackageName() { + return ""; + } + + static public String getHeaderHash() { + return "868254209"; + } + + static public String[] getArgumentNames() { + return new String[]{"content"}; + } // argument @ [1:2] private String content; @@ -34,7 +46,7 @@ public class fileview extends com.fizzed.rocker.runtime.DefaultRockerModel { static public fileview template(String content) { return new fileview() - .content(content); + .content(content); } @Override @@ -45,7 +57,7 @@ public class fileview extends com.fizzed.rocker.runtime.DefaultRockerModel { static public class Template extends com.fizzed.rocker.runtime.DefaultRockerTemplate { - // <!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\"\n name=\"viewport\">\n <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\">\n <title>Fileview</title>\n</head>\n<body>\n<textarea disabled style=\"border: none; background-color: white; width: 100vw; height: 100vh\">\n + // <!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\"\n name=\"viewport\">\n <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\">\n <title>Fileview</title>\n</head>\n<body>\n<textarea disabled style=\"border: none; background-color: white; width: 100vw; height: 100vh\">\n static private final byte[] PLAIN_TEXT_0_0; // \n</textarea>\n</body>\n</html>\n static private final byte[] PLAIN_TEXT_1_0; |