dc272bec10060ea17b3cb0b72cd93f1e3a69e9aa
[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.utils.isPOST
4 import net.pterodactylus.sone.web.pages.SoneTemplatePage
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 delete an {@link Image}.
12  */
13 class DeleteImagePage(template: Template, webInterface: WebInterface):
14                 SoneTemplatePage("deleteImage.html", template, "Page.DeleteImage.Title", webInterface, true) {
15
16         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
17                 if (request.isPOST) {
18                         val image = webInterface.core.getImage(request.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
19                         if (!image.sone.isLocal) {
20                                 throw RedirectException("noPermission.html")
21                         }
22                         if (request.httpRequest.isPartSet("abortDelete")) {
23                                 throw RedirectException("imageBrowser.html?image=${image.id}")
24                         }
25                         webInterface.core.deleteImage(image)
26                         throw RedirectException("imageBrowser.html?album=${image.album.id}")
27                 }
28                 val image = webInterface.core.getImage(request.httpRequest.getParam("image")) ?: throw RedirectException("invalid.html")
29                 if (!image.sone.isLocal) {
30                         throw RedirectException("noPermission.html")
31                 }
32                 templateContext["image"] = image
33         }
34
35 }