Move web pages to their own package
[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.text.TextFilter
5 import net.pterodactylus.sone.utils.isPOST
6 import net.pterodactylus.sone.web.pages.SoneTemplatePage
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
12 /**
13  * Page that lets the user create a new album.
14  */
15 class CreateAlbumPage(template: Template, webInterface: WebInterface):
16                 SoneTemplatePage("createAlbum.html", template, "Page.CreateAlbum.Title", webInterface, true) {
17
18         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
19                 if (request.isPOST) {
20                         val name = request.httpRequest.getPartAsStringFailsafe("name", 64).trim()
21                         if (name.isEmpty()) {
22                                 templateContext["nameMissing"] = true
23                                 return
24                         }
25                         val description = request.httpRequest.getPartAsStringFailsafe("description", 256).trim()
26                         val currentSone = webInterface.getCurrentSoneCreatingSession(request.toadletContext)
27                         val parentId = request.httpRequest.getPartAsStringFailsafe("parent", 36)
28                         val parent = if (parentId == "") currentSone.rootAlbum else webInterface.core.getAlbum(parentId)
29                         val album = webInterface.core.createAlbum(currentSone, parent)
30                         try {
31                                 album.modify().apply {
32                                         setTitle(name)
33                                         setDescription(TextFilter.filter(request.httpRequest.getHeader("Host"), description))
34                                 }.update()
35                         } catch (e: AlbumTitleMustNotBeEmpty) {
36                                 throw RedirectException("emptyAlbumTitle.html")
37                         }
38                         webInterface.core.touchConfiguration()
39                         throw RedirectException("imageBrowser.html?album=${album.id}")
40                 }
41         }
42
43 }