diff options
author | Marvin Borner | 2019-04-16 14:57:33 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-16 14:57:33 +0200 |
commit | a32ec653bf2acc777d64803189a03a4b07b24096 (patch) | |
tree | 25f199a5b15df8d333434e40a49d73f5274b6a10 /src/main/kotlin/App.kt | |
parent | 0177a6d90f3310d81eda66193ee4f4832b6e4bbd (diff) |
Added file sharing feature
Diffstat (limited to 'src/main/kotlin/App.kt')
-rw-r--r-- | src/main/kotlin/App.kt | 46 |
1 files changed, 46 insertions, 0 deletions
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)) } } @@ -334,6 +344,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 */ enum class Roles : Role { |