11efd94f6184ac217668d34bc6299d2f48549e07
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / DeleteImagePage.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 Image}.
14  */
15 class DeleteImagePage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders):
16                 LoggedInPage("deleteImage.html", template, "Page.DeleteImage.Title", webInterface, loaders) {
17
18         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 if (soneRequest.isPOST) {
20                         val image = soneRequest.core.getImage(soneRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
21                         if (!image.sone.isLocal) {
22                                 throw RedirectException("noPermission.html")
23                         }
24                         if (soneRequest.httpRequest.isPartSet("abortDelete")) {
25                                 throw RedirectException("imageBrowser.html?image=${image.id}")
26                         }
27                         soneRequest.core.deleteImage(image)
28                         throw RedirectException("imageBrowser.html?album=${image.album.id}")
29                 }
30                 val image = soneRequest.core.getImage(soneRequest.httpRequest.getParam("image")) ?: throw RedirectException("invalid.html")
31                 if (!image.sone.isLocal) {
32                         throw RedirectException("noPermission.html")
33                 }
34                 templateContext["image"] = image
35         }
36
37 }