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 | |
parent | 0177a6d90f3310d81eda66193ee4f4832b6e4bbd (diff) |
Added file sharing feature
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/App.kt | 46 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 34 |
2 files changed, 80 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 { diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 088f342..7b229f4 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -19,6 +19,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { val path = text("path").uniqueIndex() val userId = integer("userId").references(UserData.id) val accessId = varchar("accessId", 64).uniqueIndex() // TODO: Add file sharing + val isShared = bool("isShared").default(false) } /** @@ -237,6 +238,39 @@ class DatabaseController(dbFileLocation: String = "main.db") { } /** + * Returns the accessId of the given File + */ + fun getAccessId(fileLocation: String, userId: Int): String { + return transaction { + try { + FileLocation.update({ (FileLocation.userId eq userId) and (FileLocation.path eq fileLocation) }) { + it[isShared] = true + } + FileLocation.select { (FileLocation.path eq fileLocation) and (FileLocation.userId eq userId) }.map { it[FileLocation.accessId] }[0] + } catch (_: Exception) { + "" + } + } + } + + /** + * Gets the shared file via [accessId] + */ + fun getSharedFile(accessId: String): Pair<Int, String> { + return transaction { + try { + if (FileLocation.select { FileLocation.accessId eq accessId }.map { it[FileLocation.isShared] }[0]) + FileLocation.select { FileLocation.accessId eq accessId }.map { it[FileLocation.userId] to it[FileLocation.path] }[0] + else + Pair(-1, "") + } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { + log.warning("File does not exist!") + Pair(-1, "") + } + } + } + + /** * Checks whether the site has been set up */ fun isSetup(): Boolean { |