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