aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorMarvin Borner2019-05-16 18:07:53 +0200
committerMarvin Borner2019-05-16 18:07:53 +0200
commit5cbb9d1ffe6ad4b7d89c0b189a7821d1005bbdda (patch)
treedcc1c61837d92e4e1224b79295075d43d1989b0e /src/main/kotlin
parentf98205bb8215de29aacf5bc6591734af2d4c4d12 (diff)
Fixed many issues with directory uploading and sharing
Co-authored-by: LarsVomMars <lars@kroenner.eu>
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/App.kt5
-rw-r--r--src/main/kotlin/DatabaseController.kt2
-rw-r--r--src/main/kotlin/FileController.kt45
3 files changed, 42 insertions, 10 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index 3607702..12b70db 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -147,6 +147,11 @@ fun main(args: Array<String>) {
post("/upload/*", fileController::upload, roles(Roles.USER))
/**
+ * Indexes every file of the user into the database
+ */
+ get("/index", fileController::indexAll, roles(Roles.USER))
+
+ /**
* Deletes file
*/
post("/delete/*", fileController::delete, roles(Roles.USER))
diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt
index 255b6e4..481d23b 100644
--- a/src/main/kotlin/DatabaseController.kt
+++ b/src/main/kotlin/DatabaseController.kt
@@ -322,7 +322,7 @@ class DatabaseController {
}
true
} else {
- if (!isDirectoryBool) log.warning("File already exists!")
+ if (!isDirectoryBool && debug) log.warning("File already exists!")
false
}
} catch (err: Exception) {
diff --git a/src/main/kotlin/FileController.kt b/src/main/kotlin/FileController.kt
index 070679f..6e13f87 100644
--- a/src/main/kotlin/FileController.kt
+++ b/src/main/kotlin/FileController.kt
@@ -98,13 +98,11 @@ class FileController {
* Saves multipart media data into requested directory
*/
fun upload(ctx: Context) {
-
ctx.uploadedFiles("file").forEach { (_, content, name, _) ->
val fixedName = name.replace(":", "/") // "fix" for Firefox..
val userId = userHandler.getVerifiedUserId(ctx)
var addPath = ""
- // Directory sharing
fixedName.split("/").forEach {
addPath += "$it/"
if (!fixedName.endsWith(it)) databaseController.addFile(addPath, userId, true)
@@ -117,25 +115,53 @@ class FileController {
)
}
}
+
+ ctx.json("success")
}
- /*
+ /**
+ * Re-indexes every file in the users directory
+ */
fun indexAll(ctx: Context) {
- Files.list(Paths.get("$fileHome/${getVerifiedUserId(ctx)}").forEach {
- // TODO: Add file indexing function
+ val userId = userHandler.getVerifiedUserId(ctx)
+
+ fun recursiveIndex(filePath: String = "") {
+ Files.list(Paths.get("$fileHome/$userId$filePath")).forEach {
+ val filename = it.toString().drop("$fileHome/$userId".length + 1)
+
+ if (it.toFile().isDirectory) {
+ databaseController.addFile("$filename/", userId, true)
+ recursiveIndex("/$filename")
+ } else databaseController.addFile(filename, userId, false)
+ }
}
+
+ recursiveIndex()
}
- */
/**
* Deletes the requested file
*/
- fun delete(ctx: Context) { // TODO: Fix deleting of directories
+ fun delete(ctx: Context) {
val userId = userHandler.getVerifiedUserId(ctx)
if (userId > 0) {
val path = ctx.splat(0) ?: ""
- File("$fileHome/$userId/$path").delete() // File.deleteRecursively() kind of "crashes" server but deletes folder :'(
- databaseController.deleteFile(path, userId) // kind of works for deleting directories
+ val file = File("$fileHome/$userId/$path")
+
+ fun deleteDirectory(recursiveFile: File) {
+ val fileList = recursiveFile.listFiles()
+ if (fileList != null) {
+ for (subFile in fileList) {
+ deleteDirectory(subFile)
+ }
+ }
+ recursiveFile.delete()
+ }
+
+ if (file.isDirectory) {
+ deleteDirectory(file)
+ } else file.delete()
+ databaseController.deleteFile(path, userId)
}
}
@@ -146,6 +172,7 @@ class FileController {
val userId = userHandler.getVerifiedUserId(ctx)
val shareType = ctx.queryParam("type").toString()
val firstParam = ctx.splat(0) ?: ""
+ log.info(shareType)
if (userId > 0) {
val path = "$firstParam${if (shareType == "dir") "/" else ""}"
val accessId = databaseController.getAccessId(path, userId)