aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/FileController.kt
blob: d8f7dfb5c69d05c662d65730539f7857b2c639f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package space.anity

import io.javalin.*
import io.javalin.core.util.*
import io.javalin.rendering.template.*
import io.javalin.rendering.template.TemplateUtil.model
import java.io.*
import java.nio.charset.*
import java.nio.file.*
import java.text.*

class FileController {
    /**
     * Crawls the requested file and either renders the directory view or the file view
     */
    fun crawl(ctx: Context) {
        try {
            val usersFileHome = "$fileHome/${userHandler.getVerifiedUserId(ctx)}"
            File(usersFileHome).mkdirs()
            when {
                File("$usersFileHome/${ctx.splats()[0]}").isDirectory -> {
                    val files = ArrayList<Array<String>>()
                    Files.list(Paths.get("$usersFileHome/${ctx.splats()[0]}/")).forEach {
                        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.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(),
                                file.isDirectory.toString(),
                                fileSize.toString(), // unformatted file size
                                file.lastModified().toString() // unformatted last modified date
                            )
                        )
                    }
                    //files.sortWith(String.CASE_INSENSITIVE_ORDER) // TODO: Reimplement file array sorting in backend
                    ctx.render(
                        "files.rocker.html", TemplateUtil.model(
                            "files", files,
                            "path", ctx.splats()[0]
                        )
                    )
                }
                isHumanReadable(File("$usersFileHome/${ctx.splats()[0]}")) ->
                    ctx.render(
                        "fileview.rocker.html", TemplateUtil.model(
                            "content", Files.readAllLines(
                                Paths.get("$usersFileHome/${ctx.splats()[0]}"),
                                Charsets.UTF_8
                            ).joinToString(separator = "\n"),
                            "filename", File("$usersFileHome/${ctx.splats()[0]}").name,
                            "extension", File("$usersFileHome/${ctx.splats()[0]}").extension
                        )
                    )
                else -> {
                    ctx.contentType(Files.probeContentType(Paths.get("$usersFileHome/${ctx.splats()[0]}")))
                    ctx.result(FileInputStream(File("$usersFileHome/${ctx.splats()[0]}")))
                }
            }
        } catch (_: Exception) {
            throw NotFoundResponse("Error: File or directory does not exist.")
        }
    }

    /**
     * Gets directory size recursively
     */
    private 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
    }

    /**
     * Checks whether the file is binary or human-readable (text)
     */
    private fun isHumanReadable(file: File): Boolean {
        val input = FileInputStream(file)
        var size = input.available()
        if (size > 1000) size = 1000
        val data = ByteArray(size)
        input.read(data)
        input.close()
        val text = String(data, Charset.forName("ISO-8859-1"))
        val replacedText = text.replace(
            ("[a-zA-Z0-9ßöäü\\.\\*!\"§\\$\\%&/()=\\?@~'#:,;\\+><\\|\\[\\]\\{\\}\\^°²³\\\\ \\n\\r\\t_\\-`´âêîôÂÊÔÎáéíóàèìòÁÉÍÓÀÈÌÒ©‰¢£¥€±¿»«¼½¾™ª]").toRegex(),
            ""
        )
        val d = (text.length - replacedText.length).toDouble() / text.length.toDouble()
        return d > 0.95
    }

    private fun humanReadableBytes(bytes: Long): String {
        val unit = 1024
        if (bytes < unit) return "$bytes B"
        val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt()
        val pre = "KMGTPE"[exp - 1] + "i"
        return String.format("%.1f %sB", bytes / Math.pow(unit.toDouble(), exp.toDouble()), pre)
    }

    /**
     * Saves multipart media data into requested directory
     */
    fun upload(ctx: Context) {
        ctx.uploadedFiles("file").forEach { (_, content, name, _) ->
            val path = "${ctx.splats()[0]}/$name"
            val uid = userHandler.getVerifiedUserId(ctx)
            var addPath = ""
            path.split("/").forEach {
                addPath += "$it/"
                if (!path.endsWith(it)) databaseController.addFile(addPath, uid, true)
            }
            if (databaseController.addFile(path, uid)) {
                FileUtil.streamToFile(
                    content,
                    "$fileHome/$uid/$path"
                )
            }
        }
    }

    /*
    fun indexAll(ctx: Context) {
        Files.list(Paths.get("$fileHome/${getVerifiedUserId(ctx)}").forEach {
            // TODO: Add file indexing function
        }
    }
    */

    /**
     * Deletes the requested file
     */
    fun delete(ctx: Context) { // TODO: Fix deleting of directories
        val userId = userHandler.getVerifiedUserId(ctx)
        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)
        }
    }

    /**
     * Shares the requested file via the accessId
     */
    fun share(ctx: Context) {
        val userId = userHandler.getVerifiedUserId(ctx)
        val shareType = ctx.queryParam("type").toString()
        if (userId > 0) {
            val path =
                "${(if (ctx.splats()[0].startsWith("/")) ctx.splats()[0] else "/${ctx.splats()[0]}")}${if (shareType == "dir") "/" else ""}"
            val accessId = databaseController.getAccessId(path, userId)
            ctx.result("${ctx.host()}/shared?id=$accessId")
        }
    }

    /**
     * Renders the shared file
     */
    fun renderShared(ctx: Context) {
        val accessId = ctx.queryParam("id").toString()
        val sharedFileData = databaseController.getSharedFile(accessId)
        if (sharedFileData.userId > 0 && sharedFileData.fileLocation.isNotEmpty()) {
            val sharedFileLocation = "$fileHome/${sharedFileData.userId}/${sharedFileData.fileLocation}"
            if (!sharedFileData.isDirectory) {
                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.contentType(Files.probeContentType(Paths.get(sharedFileLocation)))
                    ctx.result(FileInputStream(File(sharedFileLocation)))
                }
            } else {
                // TODO: Add support for accessing files in shared directories
                // TODO: Combine the two file-crawling-render functions
                val files = ArrayList<Array<String>>()
                Files.list(Paths.get(sharedFileLocation)).forEach {
                    val filename = it.toString()
                        .drop(sharedFileLocation.length - 1)
                    val filePath = "$sharedFileLocation$filename"
                    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.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(),
                            file.isDirectory.toString(),
                            fileSize.toString(), // unformatted file size
                            file.lastModified().toString() // unformatted last modified date
                        )
                    )
                }
                //files.sortWith(String.CASE_INSENSITIVE_ORDER) // TODO: Reimplement file array sorting in backend
                ctx.render(
                    "files.rocker.html", TemplateUtil.model(
                        "files", files,
                        "path", sharedFileData.fileLocation
                    )
                )
            }
        } else {
            log.info("Unknown file!")
        }
    }
}