31813551d5017e26261ba2f5467267005217a0d4
[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(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
20                 if ("album" in freenetRequest.parameters) {
21                         templateContext["albumRequested"] = true
22                         templateContext["album"] = webInterface.core.getAlbum(freenetRequest.parameters["album"]!!)
23                         templateContext["page"] = freenetRequest.parameters["page"]
24                 } else if ("image" in freenetRequest.parameters) {
25                         templateContext["imageRequested"] = true
26                         templateContext["image"] = webInterface.core.getImage(freenetRequest.parameters["image"])
27                 } else if (freenetRequest.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                                                 Pagination(albums, webInterface.core.preferences.imagesPerPage).apply { page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0 }.also { pagination ->
37                                                         templateContext["albumPagination"] = pagination
38                                                         templateContext["albums"] = pagination.items
39                                                 }
40                                         }
41                 } else {
42                         templateContext["soneRequested"] = true
43                         templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")).orNull() ?: getCurrentSone(freenetRequest.toadletContext)
44                 }
45         }
46
47         override fun isLinkExcepted(link: URI?) = true
48
49 }