b1969cc78858ee4ed6a5bba42597f724ade49474
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / DeleteAlbumPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.utils.isPOST
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10 import javax.inject.Inject
11
12 /**
13  * Page that lets the user delete an {@link Album}.
14  */
15 class DeleteAlbumPage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
16                 LoggedInPage("deleteAlbum.html", template, "Page.DeleteAlbum.Title", webInterface, loaders, templateRenderer) {
17
18         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 if (soneRequest.isPOST) {
20                         val album = soneRequest.core.getAlbum(soneRequest.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html")
21                         if (!album.sone.isLocal) {
22                                 throw RedirectException("noPermission.html")
23                         }
24                         if (soneRequest.httpRequest.getPartAsStringFailsafe("abortDelete", 4) == "true") {
25                                 throw RedirectException("imageBrowser.html?album=${album.id}")
26                         }
27                         soneRequest.core.deleteAlbum(album)
28                         throw RedirectException(if (album.parent.isRoot) "imageBrowser.html?sone=${album.sone.id}" else "imageBrowser.html?album=${album.parent.id}")
29                 }
30                 val album = soneRequest.core.getAlbum(soneRequest.httpRequest.getParam("album"))
31                 templateContext["album"] = album ?: throw RedirectException("invalid.html")
32         }
33
34 }