diff options
author | Marvin Borner | 2019-04-15 23:24:06 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-15 23:24:06 +0200 |
commit | ce627b7b74b2a7b5ac53322df5945e00a8462b24 (patch) | |
tree | 1b0be309e72f3c1bb34c56c27122fc64caac82c7 | |
parent | 6904e09a650d71925d54f12bc8da9ea80ce0565f (diff) |
Added directory size evaluation
-rw-r--r-- | src/main/kotlin/App.kt | 32 | ||||
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 6 |
2 files changed, 26 insertions, 12 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt index 1d027e7..dd1cfb4 100644 --- a/src/main/kotlin/App.kt +++ b/src/main/kotlin/App.kt @@ -19,6 +19,7 @@ import java.util.* import java.util.logging.* import kotlin.math.* + const val fileHome = "files" val databaseController = DatabaseController() private val log = Logger.getLogger("App.kt") @@ -145,15 +146,17 @@ fun crawlFiles(ctx: Context) { val fileName = it.toString() .drop(usersFileHome.length + (if (ctx.splats()[0].isNotEmpty()) ctx.splats()[0].length + 2 else 1)) val filePath = "$usersFileHome${it.toString().drop(usersFileHome.length)}" + val file = File(filePath) + val fileSize = if (file.isDirectory) getDirectorySize(file) else file.length() files.add( // TODO: Clean up file array responses arrayOf( - if (File(filePath).isDirectory) "$fileName/" else fileName, - humanReadableBytes(File(filePath).length()), // TODO: Fix file size for directories - SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(File(filePath).lastModified()).toString(), - if (File(filePath).isDirectory) "true" else isHumanReadable(filePath).toString(), - File(filePath).length().toString(), // unformatted file size - File(filePath).lastModified().toString() // unformatted last modified date + if (file.isDirectory) "$fileName/" else fileName, + humanReadableBytes(fileSize), + SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(file.lastModified()).toString(), + if (file.isDirectory) "true" else isHumanReadable(file).toString(), + fileSize.toString(), // unformatted file size + file.lastModified().toString() // unformatted last modified date ) ) } @@ -165,7 +168,7 @@ fun crawlFiles(ctx: Context) { ) ) } - isHumanReadable("$usersFileHome/${ctx.splats()[0]}") -> + isHumanReadable(File("$usersFileHome/${ctx.splats()[0]}")) -> ctx.render( "fileview.rocker.html", model( "content", Files.readAllLines( @@ -184,6 +187,18 @@ fun crawlFiles(ctx: Context) { } /** + * Gets directory size recursively + */ +fun getDirectorySize(directory: File): Long { + var length: Long = 0 + for (file in directory.listFiles()!!) { + length += if (file.isFile) file.length() + else getDirectorySize(file) + } + return length +} + +/** * Saves multipart media data into requested directory */ fun upload(ctx: Context) { @@ -197,8 +212,7 @@ fun upload(ctx: Context) { /** * Checks whether the file is binary or human-readable (text) */ -private fun isHumanReadable(filePath: String): Boolean { - val file = File(filePath) +private fun isHumanReadable(file: File): Boolean { val input = FileInputStream(file) var size = input.available() if (size > 1000) size = 1000 diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 305c405..088f342 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -92,7 +92,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { val usersId = UserData.insert { it[username] = usernameString it[password] = BCrypt.withDefaults().hashToString(12, passwordString.toCharArray()) - it[verification] = generateRandomString(64) + it[verification] = generateRandomString() }[UserData.id] UserRoles.insert { roles -> @@ -215,7 +215,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { FileLocation.insert { it[path] = fileLocation it[userId] = usersId - it[accessId] = generateRandomString(64) + it[accessId] = generateRandomString() } } catch (err: org.jetbrains.exposed.exceptions.ExposedSQLException) { log.warning("File already exists!") @@ -316,7 +316,7 @@ class DatabaseController(dbFileLocation: String = "main.db") { /** * Generates a random string with [length] characters */ - private fun generateRandomString(length: Int): String { + private fun generateRandomString(length: Int = 64): String { val allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz0123456789" return (1..length) .map { allowedChars.random() } |