diff options
author | Marvin Borner | 2019-04-19 22:07:22 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-19 22:07:22 +0200 |
commit | 5523bd7a5e30b09baa6bb3ccd23252d0ebef6000 (patch) | |
tree | 9e0ec38ac5f0ac3dc208eb2be0bc0eef24d5a5e0 | |
parent | fe97c12731193211caf8652a6fd40e02d0d429b4 (diff) |
Fixed recursive upload function
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rw-r--r-- | src/main/kotlin/App.kt | 4 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 8 | ||||
-rw-r--r-- | src/main/kotlin/FileController.kt | 13 | ||||
-rw-r--r-- | src/main/kotlin/UserHandler.kt | 2 | ||||
-rw-r--r-- | src/main/resources/js/files.js | 34 |
5 files changed, 37 insertions, 24 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index f081bea..d3f0cd9 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -17,11 +17,9 @@ const val fileHome = "files" val databaseController = DatabaseController() val userHandler = UserHandler() val fileController = FileController() -val log = Logger.getLogger("App.kt") +private val log = Logger.getLogger("App.kt") fun main() { - log.warning(File(".").absolutePath) - val app = Javalin.create().apply { enableStaticFiles("${File(".").absolutePath}/src/main/resources/", Location.EXTERNAL) port(7000) diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 77f2e70..d65e800 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -222,7 +222,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { } true } catch (err: org.jetbrains.exposed.exceptions.ExposedSQLException) { - log.warning("File already exists!") + if (!isDirectoryBool) log.warning("File already exists!") false } } @@ -235,7 +235,8 @@ class DatabaseController(dbFileLocation: String = "main.db") { fun deleteFile(fileLocation: String, userId: Int) { transaction { try { - FileLocation.deleteWhere { (FileLocation.path eq fileLocation) and (FileLocation.userId eq userId) } + // TODO: Think of new solution for directory deleting (instead of wildcards) + FileLocation.deleteWhere { (FileLocation.path like "$fileLocation%") and (FileLocation.userId eq userId) } } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { log.warning("File does not exist!") } @@ -272,8 +273,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { val isDir = FileLocation.select { FileLocation.accessId eq accessId }.map { it[FileLocation.isDirectory] }[0] ReturnFileData(userId, fileLocation, isDir) - } - else + } else ReturnFileData(-1, "", false) } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { log.warning("File does not exist!") diff --git a/src/main/kotlin/FileController.kt b/src/main/kotlin/FileController.kt index d8f7dfb..06c1402 100644 --- a/src/main/kotlin/FileController.kt +++ b/src/main/kotlin/FileController.kt @@ -8,8 +8,11 @@ import java.io.* import java.nio.charset.* import java.nio.file.* import java.text.* +import java.util.logging.* class FileController { + private val log = Logger.getLogger(this.javaClass.name) + /** * Crawls the requested file and either renders the directory view or the file view */ @@ -113,16 +116,16 @@ class FileController { fun upload(ctx: Context) { ctx.uploadedFiles("file").forEach { (_, content, name, _) -> val path = "${ctx.splats()[0]}/$name" - val uid = userHandler.getVerifiedUserId(ctx) + val userId = userHandler.getVerifiedUserId(ctx) var addPath = "" path.split("/").forEach { addPath += "$it/" - if (!path.endsWith(it)) databaseController.addFile(addPath, uid, true) + if (!path.endsWith(it)) databaseController.addFile(addPath, userId, true) } - if (databaseController.addFile(path, uid)) { + if (databaseController.addFile(path, userId)) { FileUtil.streamToFile( content, - "$fileHome/$uid/$path" + "$fileHome/$userId/$path" ) } } @@ -144,7 +147,7 @@ class FileController { if (userId > 0) { val path = ctx.splats()[0] File("$fileHome/$userId/$path").delete() // File.deleteRecursively() kind of "crashes" server but deletes folder :'( - databaseController.deleteFile(path, userId) + databaseController.deleteFile(path, userId) // kind of works for deleting directories } } diff --git a/src/main/kotlin/UserHandler.kt b/src/main/kotlin/UserHandler.kt index cc9a768..1309348 100644 --- a/src/main/kotlin/UserHandler.kt +++ b/src/main/kotlin/UserHandler.kt @@ -3,9 +3,11 @@ package space.anity import io.javalin.* import io.javalin.rendering.template.* import org.joda.time.* +import java.util.logging.* import kotlin.math.* class UserHandler { + private val log = Logger.getLogger(this.javaClass.name) /** * Checks and verifies users credentials and logs the user in */ diff --git a/src/main/resources/js/files.js b/src/main/resources/js/files.js index 6016ba0..69cdf32 100644 --- a/src/main/resources/js/files.js +++ b/src/main/resources/js/files.js @@ -18,10 +18,9 @@ drop.addEventListener('drop', e => { e.preventDefault(); drop.style.background = "white"; const items = e.dataTransfer.items; + const uploadedFiles = []; for (let i = 0; i < items.length; i++) { - const request = new XMLHttpRequest(); - const formData = new FormData(); const item = items[i].webkitGetAsEntry(); const file = items[i].getAsFile(); @@ -52,12 +51,18 @@ drop.addEventListener('drop', e => { } else { subItem.file(subFile => { // TODO: Add support for nested directory upload with more than 1 layer - via webkitRelativePath on firefox? - formData.append("file", subFile); - console.log(subFile); - console.log(file); - if (subFile.webkitRelativePath === "") request.open("POST", `/upload/${path}/${file.name}`); - else request.open("POST", `/upload/${path}`); - request.send(formData); + console.log(uploadedFiles); + console.log(`${path}/${file.name}/${subFile.name}`); + if (!uploadedFiles.includes(`${path}/${file.name}/${subFile.name}`)) { + const formData = new FormData(); + const request = new XMLHttpRequest(); + + uploadedFiles.push(`${path}/${file.name}/${subFile.name}`); + formData.append("file", subFile); + if (subFile.webkitRelativePath === "") request.open("POST", `/upload/${path}/${file.name}`); + else request.open("POST", `/upload/${path}`); + request.send(formData); + } }) } }; @@ -65,9 +70,14 @@ drop.addEventListener('drop', e => { if (item.isDirectory) { iterateFiles(item); } else { - formData.append("file", file); - request.open("POST", `/upload/${path}`, true); - request.send(formData); + if (!uploadedFiles.includes(`${path}/${file.name}`)) { + const formData = new FormData(); + const request = new XMLHttpRequest(); + + formData.append("file", file); + request.open("POST", `/upload/${path}`); + request.send(formData); + } } } @@ -153,7 +163,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}`); + request.open("POST", `/delete/${path}/${fileName}`, true); request.send(); parent.remove(); } else console.log("File not deleted!") |