ba16e2195cfe55bba7b8bba01534aeaf3f7e23af
[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.paginate
6 import net.pterodactylus.sone.utils.parameters
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import java.net.URI
12 import javax.inject.Inject
13
14 /**
15  * The image browser page is the entry page for the image management.
16  */
17 @MenuName("ImageBrowser")
18 class ImageBrowserPage @Inject constructor(template: Template, webInterface: WebInterface):
19                 LoggedInPage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface) {
20
21         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
22                 if ("album" in soneRequest.parameters) {
23                         templateContext["albumRequested"] = true
24                         templateContext["album"] = soneRequest.core.getAlbum(soneRequest.parameters["album"]!!)
25                         templateContext["page"] = soneRequest.parameters["page"]
26                 } else if ("image" in soneRequest.parameters) {
27                         templateContext["imageRequested"] = true
28                         templateContext["image"] = soneRequest.core.getImage(soneRequest.parameters["image"])
29                 } else if (soneRequest.parameters["mode"] == "gallery") {
30                         templateContext["galleryRequested"] = true
31                         soneRequest.core.sones
32                                         .map(Sone::getRootAlbum)
33                                         .flatMap(Album::getAlbums)
34                                         .flatMap { Album.FLATTENER.apply(it)!! }
35                                         .filterNot(Album::isEmpty)
36                                         .sortedBy(Album::getTitle)
37                                         .also { albums ->
38                                                 albums.paginate(soneRequest.core.preferences.imagesPerPage)
39                                                                 .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
40                                                                 .also { pagination ->
41                                                         templateContext["albumPagination"] = pagination
42                                                         templateContext["albums"] = pagination.items
43                                                 }
44                                         }
45                 } else {
46                         templateContext["soneRequested"] = true
47                         templateContext["sone"] = soneRequest.core.getSone(soneRequest.httpRequest.getParam("sone")) ?: currentSone
48                 }
49         }
50
51         override fun isLinkExcepted(link: URI) = true
52
53 }