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 +++++++++++++++++++++++++++++ src/main/kotlin/DatabaseController.kt | 34 +++++++++++++++++++++ src/main/resources/css/files.css | 21 +++++++++++-- src/main/resources/fonts/ionicons .eot | Bin 0 -> 112662 bytes src/main/resources/js/files.js | 31 ++++++++++++++++++- src/main/resources/js/fileview.js | 5 +++- src/main/resources/views/files.rocker.html | 5 ++++ src/main/resources/views/index.rocker.html | 12 ++++++-- 8 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/fonts/ionicons .eot (limited to 'src') 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 */ 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) } /** @@ -236,6 +237,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 { + 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 */ diff --git a/src/main/resources/css/files.css b/src/main/resources/css/files.css index 3ceb173..c7302bf 100644 --- a/src/main/resources/css/files.css +++ b/src/main/resources/css/files.css @@ -45,11 +45,15 @@ colgroup col:nth-child(2) { } colgroup col:nth-child(3) { - width: 30%; + width: 25%; } colgroup col:nth-child(4) { - width: 15%; + width: 10%; +} + +colgroup col:nth-child(5) { + width: 10%; } .drop { @@ -70,3 +74,16 @@ colgroup col:nth-child(4) { transform: scale(1.3); color: red; } + +.share { + font-size: 20px; + background-color: inherit; + border: 0; + z-index: 100; + cursor: pointer; +} + +.share:hover { + transform: scale(1.3); + color: dodgerblue; +} diff --git a/src/main/resources/fonts/ionicons .eot b/src/main/resources/fonts/ionicons .eot new file mode 100644 index 0000000..671df91 Binary files /dev/null and b/src/main/resources/fonts/ionicons .eot differ diff --git a/src/main/resources/js/files.js b/src/main/resources/js/files.js index 8b8bd87..f776165 100644 --- a/src/main/resources/js/files.js +++ b/src/main/resources/js/files.js @@ -33,7 +33,8 @@ drop.addEventListener('drop', e => { row.insertCell(0).innerHTML = file.name; row.insertCell(1).innerHTML = bytesToSize(file.size); row.insertCell(2).innerHTML = `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`; - row.insertCell(3).innerHTML = ""; + row.insertCell(3).innerHTML = ""; + row.insertCell(4).innerHTML = ""; setListeners(); @@ -128,6 +129,34 @@ function setListeners() { parent.remove(); }) }); + + // share button + document.querySelectorAll(".share").forEach(element => { + element.addEventListener("click", e => { + e.stopPropagation(); + const request = new XMLHttpRequest(); + const parent = e.target.closest("tr"); + const fileName = parent.getAttribute("data-href") || parent.getAttribute("data-path"); + + request.open("POST", `/share/${path}/${fileName}`); + request.onload = () => { + if (request.readyState === 4) { + if (request.status === 200) { + const input = document.createElement('input'); + input.setAttribute('value', request.responseText); + document.body.appendChild(input); + input.select(); + document.execCommand('copy'); + document.body.removeChild(input); + alert("Copied url to clipboard!") + } else { + alert("Something went wrong."); + } + } + }; + request.send(); + }) + }); } setListeners(); diff --git a/src/main/resources/js/fileview.js b/src/main/resources/js/fileview.js index ace7884..d78c342 100644 --- a/src/main/resources/js/fileview.js +++ b/src/main/resources/js/fileview.js @@ -1,6 +1,7 @@ const preview = document.getElementById("preview"); const content = document.getElementById("content"); -const body = document.getElementsByTagName("body")[0]; +const html = document.getElementsByTagName("html")[0]; +const body = document.body; // buttons const raw = document.getElementById("raw"); @@ -23,12 +24,14 @@ if (extension === "md" || extension === "html") { raw.addEventListener("click", () => { if (preview.style.display === "block") { + html.style.overflow = "visible"; body.style.overflow = "visible"; raw.innerText = "Show preview"; preview.style.display = "none"; content.style.display = "block"; settings.style.display = "block"; } else { + html.style.overflow = "hidden"; body.style.overflow = "hidden"; raw.innerText = "Show raw"; preview.style.display = "block"; diff --git a/src/main/resources/views/files.rocker.html b/src/main/resources/views/files.rocker.html index acbe812..0e42891 100644 --- a/src/main/resources/views/files.rocker.html +++ b/src/main/resources/views/files.rocker.html @@ -35,6 +35,7 @@ Name Size Last modified + Share Delete @@ -42,6 +43,7 @@ + @@ -55,6 +57,9 @@ @fileArray[0] @fileArray[1] @fileArray[2] + + + diff --git a/src/main/resources/views/index.rocker.html b/src/main/resources/views/index.rocker.html index 4f5d32b..a0b4035 100644 --- a/src/main/resources/views/index.rocker.html +++ b/src/main/resources/views/index.rocker.html @@ -4,10 +4,16 @@

Welcome to Kloud @username!

@if(username.length() > 0) { - + + + } else { - + + + } - + + + } -- cgit v1.2.3