✨ Add @ToadletPath annotation
[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.*
4 import net.pterodactylus.sone.data.Album.Modifier.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.text.*
7 import net.pterodactylus.sone.utils.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.template.*
11 import javax.inject.*
12
13 /**
14  * Page that lets the user create a new album.
15  */
16 @TemplatePath("/templates/createAlbum.html")
17 @ToadletPath("createAlbum.html")
18 class CreateAlbumPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
19                 LoggedInPage("createAlbum.html", "Page.CreateAlbum.Title", webInterface, loaders, templateRenderer) {
20
21         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
22                 if (soneRequest.isPOST) {
23                         val name = soneRequest.httpRequest.getPartAsStringFailsafe("name", 64).trim()
24                         if (name.isEmpty()) {
25                                 templateContext["nameMissing"] = true
26                                 return
27                         }
28                         val description = soneRequest.httpRequest.getPartAsStringFailsafe("description", 256).trim()
29                         val parentId = soneRequest.httpRequest.getPartAsStringFailsafe("parent", 36)
30                         val parent = if (parentId == "") currentSone.rootAlbum else soneRequest.core.getAlbum(parentId)
31                         val album = soneRequest.core.createAlbum(currentSone, parent)
32                         try {
33                                 album.modify().apply {
34                                         setTitle(name)
35                                         setDescription(TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), description))
36                                 }.update()
37                         } catch (e: AlbumTitleMustNotBeEmpty) {
38                                 throw RedirectException("emptyAlbumTitle.html")
39                         }
40                         soneRequest.core.touchConfiguration()
41                         throw RedirectException("imageBrowser.html?album=${album.id}")
42                 }
43         }
44
45 }