Merge branch 'release-0.9.7'
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditAlbumPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.FreenetRequest
7 import net.pterodactylus.util.template.Template
8 import net.pterodactylus.util.template.TemplateContext
9
10 /**
11  * Page that lets the user edit the name and description of an album.
12  */
13 class EditAlbumPage(template: Template, webInterface: WebInterface):
14                 SoneTemplatePage("editAlbum.html", template, "Page.EditAlbum.Title", webInterface, true) {
15
16         override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
17                 if (freenetRequest.isPOST) {
18                         val album = webInterface.core.getAlbum(freenetRequest.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html")
19                         album.takeUnless { it.sone.isLocal }?.run { throw RedirectException("noPermission.html") }
20                         if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
21                                 album.parent?.moveAlbumUp(album)
22                                 webInterface.core.touchConfiguration()
23                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
24                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
25                                 album.parent?.moveAlbumDown(album)
26                                 webInterface.core.touchConfiguration()
27                                 throw RedirectException("imageBrowser.html?album=${album.parent?.id}")
28                         } else {
29                                 try {
30                                         album.modify()
31                                                         .setTitle(freenetRequest.httpRequest.getPartAsStringFailsafe("title", 100))
32                                                         .setDescription(freenetRequest.httpRequest.getPartAsStringFailsafe("description", 1000))
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
43 }