From e7ef35277dd0b4bba5b3fb675d5eed3cd0f0fcb8 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Mon, 29 Dec 2025 11:44:57 -0800 Subject: refactor webui into templates --- src/main/java/com/pinapelz/frontend/App.kt | 1016 +------------------- .../java/com/pinapelz/frontend/HtmlTemplates.kt | 111 +++ src/main/resources/templates/directory-item.html | 17 + .../resources/templates/empty-directories.html | 6 + src/main/resources/templates/empty-files.html | 16 + .../resources/templates/empty-search-results.html | 22 + src/main/resources/templates/file-row.html | 17 + src/main/resources/templates/file-table.html | 27 + src/main/resources/templates/index.html | 573 ----------- src/main/resources/templates/main.html | 840 ++++++++++++++++ 10 files changed, 1061 insertions(+), 1584 deletions(-) create mode 100644 src/main/java/com/pinapelz/frontend/HtmlTemplates.kt create mode 100644 src/main/resources/templates/directory-item.html create mode 100644 src/main/resources/templates/empty-directories.html create mode 100644 src/main/resources/templates/empty-files.html create mode 100644 src/main/resources/templates/empty-search-results.html create mode 100644 src/main/resources/templates/file-row.html create mode 100644 src/main/resources/templates/file-table.html delete mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/main.html diff --git a/src/main/java/com/pinapelz/frontend/App.kt b/src/main/java/com/pinapelz/frontend/App.kt index 26ce243..02da8b7 100644 --- a/src/main/java/com/pinapelz/frontend/App.kt +++ b/src/main/java/com/pinapelz/frontend/App.kt @@ -1,5 +1,7 @@ package com.pinapelz.frontend +import com.pinapelz.frontend.HtmlTemplates + import io.javalin.Javalin import io.javalin.http.staticfiles.Location import com.pinapelz.Retriever @@ -229,893 +231,11 @@ fun validateDirectoryName(path: String): String? { } fun generateMainHtml(directoryId: Int): String { - return """ - - - - - - nitro-fs - - - - - -
-
-
-
- - nitro-fs -
-
# loading...
-
-
- - -
-
- -
- - - - -
-
- - - - - loading - - files -
-
- -
-
-
- -
-

loading files...

-

fetching your files from discord storage

-
-
-
- - - - - - - - - """.trimIndent() + return HtmlTemplates.generateMainPage(directoryId) } fun generateDirectoryListHtml(directories: List>): String { - if (directories.isEmpty()) { - return """ -
-
- -
-

no directories found

-
- """.trimIndent() - } - - val directoryItems = directories.joinToString("") { dir -> - val path = dir["path"] as String - val displayName = if (path.isEmpty()) "root" else path - val fileCount = dir["fileCount"] as Int - val countDisplay = if (fileCount == 0) "" else " (${fileCount})" - - """ -
-
-
- -
-
-
$displayName
-
- $fileCount files -
-
-
- -
-
- ${if (!path.isEmpty()) """ -
- -
- """ else ""} -
- """.trimIndent() - } - - return directoryItems + return HtmlTemplates.generateDirectoryList(directories) } fun formatFileSize(bytes: Long): String { @@ -1129,131 +249,5 @@ fun formatFileSize(bytes: Long): String { } fun generateFileTableHtml(files: List>, search: String = "", mimeTypeFilter: String = ""): String { - fun getFileIcon(mimeType: String?): String { - if (mimeType == null) return "fas fa-file" - - return when { - mimeType.startsWith("image/") -> "fas fa-file-image" - mimeType.startsWith("video/") -> "fas fa-file-video" - mimeType.startsWith("audio/") -> "fas fa-file-audio" - mimeType.contains("pdf") -> "fas fa-file-pdf" - mimeType.startsWith("text/") -> "fas fa-file-alt" - mimeType.contains("zip") || mimeType.contains("tar") || mimeType.contains("rar") -> "fas fa-file-archive" - else -> "fas fa-file" - } - } - - val fileRows = files.joinToString("") { file -> - """ - - - - - ${file["name"]} - - - ${file["description"]} - ${file["size"]} - ${(file["mimeType"] as? String)?.split("/")?.get(0) ?: "file"} - ${(file["created"] as String).split(" ")[0]} - - - - - """.trimIndent() - } - - return if (files.isEmpty()) { - val emptyMessage = if (search.isNotEmpty() || mimeTypeFilter.isNotEmpty()) { - """ -
-
- -
-

no matches found

-

try different search terms or clear your filters

- -
- """ - } else { - """ -
-
- -
-

no files yet

-

upload some files through discord to see them here

-
- """ - } - - """ - $emptyMessage - - """.trimIndent() - } else { - """ - - - - - - - - - - - - - $fileRows - -
namedescriptionsizetypedateactions
- - """.trimIndent() - } + return HtmlTemplates.generateFileTable(files, search, mimeTypeFilter) } diff --git a/src/main/java/com/pinapelz/frontend/HtmlTemplates.kt b/src/main/java/com/pinapelz/frontend/HtmlTemplates.kt new file mode 100644 index 0000000..21c8033 --- /dev/null +++ b/src/main/java/com/pinapelz/frontend/HtmlTemplates.kt @@ -0,0 +1,111 @@ +package com.pinapelz.frontend + +import java.io.InputStream + +object HtmlTemplates { + + private fun loadTemplate(templatePath: String): String { + val inputStream: InputStream = this::class.java.classLoader.getResourceAsStream("templates/$templatePath") + ?: throw IllegalArgumentException("Template not found: $templatePath") + return inputStream.bufferedReader().use { it.readText() } + } + + private fun String.substitute(vararg pairs: Pair): String { + var result = this + for ((placeholder, value) in pairs) { + result = result.replace("{{$placeholder}}", value.toString()) + } + return result + } + + fun generateMainPage(directoryId: Int): String { + val template = loadTemplate("main.html") + return template.substitute( + "directoryId" to directoryId + ) + } + + fun generateDirectoryList(directories: List>): String { + if (directories.isEmpty()) { + return loadTemplate("empty-directories.html") + } + + val directoryTemplate = loadTemplate("directory-item.html") + val directoryItems = directories.joinToString("") { dir -> + val path = dir["path"] as String + val displayName = if (path.isEmpty()) "root" else path + val fileCount = dir["fileCount"] as Int + val iconClass = if (path.isEmpty()) "fa-home" else "fa-folder" + val deleteButton = if (path.isNotEmpty()) { + """ +
+ +
+ """.trimIndent() + } else "" + + directoryTemplate.substitute( + "id" to dir["id"]!!, + "path" to path.replace("'", "\\'"), + "displayName" to displayName, + "iconClass" to iconClass, + "fileCount" to fileCount, + "deleteButton" to deleteButton + ) + } + + return directoryItems + } + + fun generateFileTable(files: List>, search: String = "", mimeTypeFilter: String = ""): String { + if (files.isEmpty()) { + return if (search.isNotEmpty() || mimeTypeFilter.isNotEmpty()) { + loadTemplate("empty-search-results.html") + } else { + loadTemplate("empty-files.html") + } + } + + val fileRowTemplate = loadTemplate("file-row.html") + val fileRows = files.joinToString("") { file -> + val mimeType = file["mimeType"] as? String + val fileIcon = getFileIcon(mimeType) + val fileName = file["name"] as String + val fileType = mimeType?.split("/")?.get(0) ?: "file" + val createdDate = (file["created"] as String).split(" ")[0] + + fileRowTemplate.substitute( + "id" to file["id"]!!, + "fileIcon" to fileIcon, + "name" to fileName, + "description" to (file["description"] ?: ""), + "size" to (file["size"] ?: ""), + "fileType" to fileType, + "createdDate" to createdDate, + "escapedName" to fileName.replace("'", "\\'") + ) + } + + val tableTemplate = loadTemplate("file-table.html") + return tableTemplate.substitute( + "fileRows" to fileRows, + "fileCount" to files.size + ) + } + + private fun getFileIcon(mimeType: String?): String { + if (mimeType == null) return "fas fa-file" + + return when { + mimeType.startsWith("image/") -> "fas fa-file-image" + mimeType.startsWith("video/") -> "fas fa-file-video" + mimeType.startsWith("audio/") -> "fas fa-file-audio" + mimeType.contains("pdf") -> "fas fa-file-pdf" + mimeType.startsWith("text/") -> "fas fa-file-alt" + mimeType.contains("zip") || mimeType.contains("tar") || mimeType.contains("rar") -> "fas fa-file-archive" + else -> "fas fa-file" + } + } +} \ No newline at end of file diff --git a/src/main/resources/templates/directory-item.html b/src/main/resources/templates/directory-item.html new file mode 100644 index 0000000..391bcca --- /dev/null +++ b/src/main/resources/templates/directory-item.html @@ -0,0 +1,17 @@ +
+
+
+ +
+
+
{{displayName}}
+
+ {{fileCount}} files +
+
+
+ +
+
+ {{deleteButton}} +
\ No newline at end of file diff --git a/src/main/resources/templates/empty-directories.html b/src/main/resources/templates/empty-directories.html new file mode 100644 index 0000000..665dbd8 --- /dev/null +++ b/src/main/resources/templates/empty-directories.html @@ -0,0 +1,6 @@ +
+
+ +
+

no directories found

+
\ No newline at end of file diff --git a/src/main/resources/templates/empty-files.html b/src/main/resources/templates/empty-files.html new file mode 100644 index 0000000..d5ca35a --- /dev/null +++ b/src/main/resources/templates/empty-files.html @@ -0,0 +1,16 @@ +
+
+ +
+

no files yet

+

upload some files through discord to see them here

+
+ \ No newline at end of file diff --git a/src/main/resources/templates/empty-search-results.html b/src/main/resources/templates/empty-search-results.html new file mode 100644 index 0000000..a26aa37 --- /dev/null +++ b/src/main/resources/templates/empty-search-results.html @@ -0,0 +1,22 @@ +
+
+ +
+

no matches found

+

try different search terms or clear your filters

+ +
+ \ No newline at end of file diff --git a/src/main/resources/templates/file-row.html b/src/main/resources/templates/file-row.html new file mode 100644 index 0000000..b6219eb --- /dev/null +++ b/src/main/resources/templates/file-row.html @@ -0,0 +1,17 @@ + + + + + {{name}} + + + {{description}} + {{size}} + {{fileType}} + {{createdDate}} + + + + \ No newline at end of file diff --git a/src/main/resources/templates/file-table.html b/src/main/resources/templates/file-table.html new file mode 100644 index 0000000..02b9553 --- /dev/null +++ b/src/main/resources/templates/file-table.html @@ -0,0 +1,27 @@ + + + + + + + + + + + + + {{fileRows}} + +
namedescriptionsizetypedateactions
+ \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html deleted file mode 100644 index 98bc45e..0000000 --- a/src/main/resources/templates/index.html +++ /dev/null @@ -1,573 +0,0 @@ - - - - - - File Storage - - - - - -
- -
-
-
- - file storage -
-
# root
-
-
- -
-
- - -
- - - - -
-
- - - - - loading - - files -
-
- - localhost:7070 -
-
- - connecting... -
-
- - -
-
-
-
- -
-

loading files...

-

fetching your files from discord storage

-
-
-
-
-
- - - - - \ No newline at end of file diff --git a/src/main/resources/templates/main.html b/src/main/resources/templates/main.html new file mode 100644 index 0000000..85e2007 --- /dev/null +++ b/src/main/resources/templates/main.html @@ -0,0 +1,840 @@ + + + + + + nitro-fs + + + + + +
+
+
+
+ + nitro-fs +
+
# loading...
+
+
+ + +
+
+ +
+ + + + +
+
+ + + + + loading + + files +
+
+ +
+
+
+
+ +
+

loading files...

+

fetching your files from discord storage

+
+
+
+
+
+ + + + \ No newline at end of file -- cgit v1.2.3