♻️ Copy session-handling code to FreenetRequest
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreateAlbumPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.text.TextFilter
6 import net.pterodactylus.sone.utils.isPOST
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 javax.inject.Inject
12
13 /**
14  * Page that lets the user create a new album.
15  */
16 class CreateAlbumPage @Inject constructor(template: Template, webInterface: WebInterface):
17                 LoggedInPage("createAlbum.html", template, "Page.CreateAlbum.Title", webInterface) {
18
19         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 if (soneRequest.isPOST) {
21                         val name = soneRequest.httpRequest.getPartAsStringFailsafe("name", 64).trim()
22                         if (name.isEmpty()) {
23                                 templateContext["nameMissing"] = true
24                                 return
25                         }
26                         val description = soneRequest.httpRequest.getPartAsStringFailsafe("description", 256).trim()
27                         val parentId = soneRequest.httpRequest.getPartAsStringFailsafe("parent", 36)
28                         val parent = if (parentId == "") currentSone.rootAlbum else soneRequest.core.getAlbum(parentId)
29                         val album = soneRequest.core.createAlbum(currentSone, parent)
30                         try {
31                                 album.modify().apply {
32                                         setTitle(name)
33                                         setDescription(TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), description))
34                                 }.update()
35                         } catch (e: AlbumTitleMustNotBeEmpty) {
36                                 throw RedirectException("emptyAlbumTitle.html")
37                         }
38                         soneRequest.core.touchConfiguration()
39                         throw RedirectException("imageBrowser.html?album=${album.id}")
40                 }
41         }
42
43 }