aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-04-20 14:49:04 +0200
committerMarvin Borner2019-04-20 14:49:04 +0200
commit034dfe305973a0f2e7deb0c9776605664ea9b169 (patch)
tree908864d3b1a08635288a02d6f0bf51ea80ebffda
parent9fe9795c996a6e37e894f7e42ba8761320c47798 (diff)
Fixed many minor things regarding stability
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rw-r--r--src/main/kotlin/App.kt7
-rw-r--r--src/main/kotlin/DatabaseController.kt4
-rw-r--r--src/main/kotlin/FileController.kt18
-rw-r--r--src/main/resources/js/files.js28
4 files changed, 33 insertions, 24 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index 50c7c18..ebdb560 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -40,12 +40,7 @@ fun main() {
* Normalizes and cleans the requested url
*/
before("/*") { ctx ->
- run {
- if (URI(ctx.url()).normalize().toString() != ctx.url()) {
- log.warning("Normalized url from ${ctx.url()} to ${URI(ctx.url()).normalize()}")
- ctx.redirect(URI(ctx.url()).normalize().toString())
- }
- }
+ if (URI(ctx.url()).normalize().toString() != ctx.url()) ctx.redirect(URI(ctx.url()).normalize().toString())
}
/**
diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt
index 888171c..b3fac60 100644
--- a/src/main/kotlin/DatabaseController.kt
+++ b/src/main/kotlin/DatabaseController.kt
@@ -274,7 +274,7 @@ class DatabaseController(dbFileLocation: String = "main.db") {
ReturnFileData(userId, fileLocation, isDir)
} else
ReturnFileData(-1, "", false)
- } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) {
+ } catch (_: Exception) {
log.warning("File does not exist!")
ReturnFileData(-1, "", false)
}
@@ -373,4 +373,4 @@ data class ReturnFileData(
val userId: Int,
val fileLocation: String,
val isDirectory: Boolean
-) // TODO: Think about using dataclass
+)
diff --git a/src/main/kotlin/FileController.kt b/src/main/kotlin/FileController.kt
index 15bf11f..e9c861c 100644
--- a/src/main/kotlin/FileController.kt
+++ b/src/main/kotlin/FileController.kt
@@ -49,7 +49,7 @@ class FileController {
"files",
files,
"path",
- (if (firstParam.firstOrNull() != '/' && firstParam.isNotEmpty()) "/$firstParam" else firstParam)
+ (if (firstParam.firstOrNull() == '/') firstParam.drop(1) else firstParam)
)
)
}
@@ -106,6 +106,9 @@ class FileController {
return d > 0.95
}
+ /**
+ * Converts bytes to human-readable text like 100MiB
+ */
private fun humanReadableBytes(bytes: Long): String {
val unit = 1024
if (bytes < unit) return "$bytes B"
@@ -118,8 +121,10 @@ class FileController {
* Saves multipart media data into requested directory
*/
fun upload(ctx: Context) {
+ val firstParam = ctx.splat(0) ?: ""
ctx.uploadedFiles("file").forEach { (_, content, name, _) ->
- val path = "${ctx.splat(0)}/$name"
+ val path = if (firstParam.isEmpty()) name else "$firstParam/$name"
+
val userId = userHandler.getVerifiedUserId(ctx)
var addPath = ""
path.split("/").forEach {
@@ -163,8 +168,7 @@ class FileController {
val shareType = ctx.queryParam("type").toString()
val firstParam = ctx.splat(0) ?: ""
if (userId > 0) {
- val path =
- "${(if (firstParam.startsWith("/")) firstParam else "/$firstParam")}${if (shareType == "dir") "/" else ""}"
+ val path = "$firstParam${if (shareType == "dir") "/" else ""}"
val accessId = databaseController.getAccessId(path, userId)
ctx.result("${ctx.host()}/shared?id=$accessId")
}
@@ -191,7 +195,8 @@ class FileController {
)
)
} else {
- ctx.contentType(Files.probeContentType(Paths.get(sharedFileLocation)))
+ val fileProbe = Files.probeContentType(Paths.get(sharedFileLocation)) // is null if file type is not recognized
+ ctx.contentType(fileProbe)
ctx.result(FileInputStream(File(sharedFileLocation)))
}
} else {
@@ -221,7 +226,8 @@ class FileController {
ctx.render(
"files.rocker.html", TemplateUtil.model(
"files", files,
- "path", sharedFileData.fileLocation
+ "path",
+ (if (sharedFileData.fileLocation.firstOrNull() != '/') "/${sharedFileData.fileLocation}" else sharedFileData.fileLocation)
)
)
}
diff --git a/src/main/resources/js/files.js b/src/main/resources/js/files.js
index 3346a31..4ef5bcc 100644
--- a/src/main/resources/js/files.js
+++ b/src/main/resources/js/files.js
@@ -51,7 +51,7 @@ drop.addEventListener('drop', e => {
} else {
subItem.file(subFile => {
// TODO: Add support for nested directory upload with more than 1 layer - via webkitRelativePath on firefox?
- if (!uploadedFiles.includes(`${path}/${file.name}/${subFile.name}`)) {
+ if (!uploadedFiles.includes(`/${path}/${file.name}/${subFile.name}`.clean())) {
const formData = new FormData();
const request = new XMLHttpRequest();
@@ -61,10 +61,10 @@ drop.addEventListener('drop', e => {
}
};
- uploadedFiles.push(`${path}/${file.name}/${subFile.name}`);
+ uploadedFiles.push(`/${path}/${file.name}/${subFile.name}`.clean());
formData.append("file", subFile);
- if (subFile.webkitRelativePath === "") request.open("POST", `/upload/${path}/${file.name}`);
- else request.open("POST", `/upload/${path}`);
+ if (subFile.webkitRelativePath === "") request.open("POST", `/upload/${path}/${file.name}`.clean());
+ else request.open("POST", `/upload/${path}`.clean());
request.send(formData);
}
})
@@ -74,7 +74,7 @@ drop.addEventListener('drop', e => {
if (item.isDirectory) {
iterateFiles(item);
} else {
- if (!uploadedFiles.includes(`${path}/${file.name}`)) {
+ if (!uploadedFiles.includes(`/${path}/${file.name}`.clean())) {
const formData = new FormData();
const request = new XMLHttpRequest();
@@ -85,7 +85,7 @@ drop.addEventListener('drop', e => {
};
formData.append("file", file);
- request.open("POST", `/upload/${path}`);
+ request.open("POST", `/upload/${path}`.clean());
request.send(formData);
}
}
@@ -124,7 +124,7 @@ function setListeners() {
element.addEventListener("click", () => {
if (images.indexOf(extension) === -1 && videos.indexOf(extension) === -1 && audio.indexOf(extension) === -1)
- window.location = `/files/${path}/${filename}`; // download binary files
+ window.location = `/files/${path}/${filename}`.clean(); // download binary files
});
});
@@ -161,7 +161,7 @@ function setListeners() {
// normal files
document.querySelectorAll("[data-href]").forEach(element => {
element.addEventListener("click", () => {
- window.location = `/files${path}/${element.getAttribute("data-href")}`;
+ window.location = `/files/${path}/${element.getAttribute("data-href")}`.clean();
})
});
@@ -173,7 +173,7 @@ function setListeners() {
const parent = e.target.closest("tr");
const fileName = parent.getAttribute("data-href") || parent.getAttribute("data-path");
if (confirm(`Do you really want to delete: ${fileName}?`)) {
- request.open("POST", `/delete/${path}/${fileName}`, true);
+ request.open("POST", `/delete/${path}/${fileName}`.clean(), true);
request.send();
parent.remove();
} else console.log("File not deleted!")
@@ -189,7 +189,7 @@ function setListeners() {
const fileName = parent.getAttribute("data-href") || parent.getAttribute("data-path");
const type = fileName.endsWith('/') ? 'dir' : 'file';
- request.open("POST", `/share/${path}/${fileName}?type=${type}`, true);
+ request.open("POST", `/share/${path}/${fileName}?type=${type}`.clean());
request.onload = () => {
if (request.readyState === 4) {
if (request.status === 200) { // TODO: fix clipboard in Firefox
@@ -239,3 +239,11 @@ document.querySelectorAll("thead tr > th").forEach((header, index) => {
header.setAttribute("data-asc", (ascending === "false").toString())
})
});
+
+/**
+ * Cleans the string (in this case the url)
+ * @returns {String}
+ */
+String.prototype.clean = function () {
+ return this.replace(/\/\/+/g, '/')
+};