From a32ec653bf2acc777d64803189a03a4b07b24096 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 16 Apr 2019 14:57:33 +0200 Subject: Added file sharing feature --- src/main/kotlin/App.kt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/main/kotlin/App.kt') diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index dd1cfb4..4572cf1 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -107,6 +107,16 @@ fun main() { * Deletes file */ post("/delete/*", ::delete, roles(Roles.USER)) + + /** + * Shares file + */ + post("/share/*", ::shareFile, roles(Roles.USER)) + + /** + * Shows the shared file + */ + get("/shared", ::renderSharedFile, roles(Roles.GUEST)) } } @@ -333,6 +343,42 @@ fun delete(ctx: Context) { } } +/** + * Shares the requested file via the accessId + */ +fun shareFile(ctx: Context) { + val userId = getVerifiedUserId(ctx) + if (userId > 0) { + val accessId = databaseController.getAccessId(ctx.splats()[0], userId) + ctx.result("${ctx.host()}/shared?id=$accessId") + } +} + +/** + * Renders the shared file + */ +fun renderSharedFile(ctx: Context) { + val accessId = ctx.queryParam("id").toString() + val sharedFileData = databaseController.getSharedFile(accessId) + if (sharedFileData.first > 0 && sharedFileData.second.isNotEmpty()) { + val sharedFileLocation = "$fileHome/${sharedFileData.first}/${sharedFileData.second}" + if (isHumanReadable(File(sharedFileLocation))) { + ctx.render( + "fileview.rocker.html", model( + "content", Files.readAllLines( + Paths.get(sharedFileLocation), + Charsets.UTF_8 + ).joinToString(separator = "\n"), + "filename", File(sharedFileLocation).name, + "extension", File(sharedFileLocation).extension + ) + ) + } else ctx.result(FileInputStream(File(sharedFileLocation))) + } else { + log.info("Unknown file!") + } +} + /** * Declares the roles in which a user can be in */ -- cgit v1.2.3