Replace upload image page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / ImageBrowserPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.utils.Pagination
6 import net.pterodactylus.sone.utils.parameters
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import java.net.URI
12
13 /**
14  * The image browser page is the entry page for the image management.
15  */
16 class ImageBrowserPage(template: Template, webInterface: WebInterface):
17                 SoneTemplatePage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true) {
18
19         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
20                 if ("album" in request.parameters) {
21                         templateContext["albumRequested"] = true
22                         templateContext["album"] = webInterface.core.getAlbum(request.parameters["album"]!!)
23                         templateContext["page"] = request.parameters["page"]
24                 } else if ("image" in request.parameters) {
25                         templateContext["imageRequested"] = true
26                         templateContext["image"] = webInterface.core.getImage(request.parameters["image"])
27                 } else if (request.parameters["mode"] == "gallery") {
28                         templateContext["galleryRequested"] = true
29                         webInterface.core.sones
30                                         .map(Sone::getRootAlbum)
31                                         .flatMap(Album::getAlbums)
32                                         .flatMap { Album.FLATTENER.apply(it)!! }
33                                         .filterNot(Album::isEmpty)
34                                         .sortedBy(Album::getTitle)
35                                         .also { albums ->
36                                                 templateContext["albums"] = albums
37                                                 templateContext["albumPagination"] = Pagination(albums, 12).apply { page = request.parameters["page"]?.toIntOrNull() ?: 0 }
38                                         }
39                 } else {
40                         templateContext["soneRequested"] = true
41                         templateContext["sone"] = webInterface.core.getSone(request.httpRequest.getParam("sone")).orNull() ?: getCurrentSone(request.toadletContext)
42                 }
43         }
44
45         override fun isLinkExcepted(link: URI?) = true
46
47 }