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