Add test for DI constructability of DeleteImagePage
[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.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 import javax.inject.Inject
10
11 /**
12  * Page that lets the user delete an {@link Image}.
13  */
14 class DeleteImagePage @Inject constructor(template: Template, webInterface: WebInterface):
15                 LoggedInPage("deleteImage.html", template, "Page.DeleteImage.Title", webInterface) {
16
17         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
18                 if (freenetRequest.isPOST) {
19                         val image = webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
20                         if (!image.sone.isLocal) {
21                                 throw RedirectException("noPermission.html")
22                         }
23                         if (freenetRequest.httpRequest.isPartSet("abortDelete")) {
24                                 throw RedirectException("imageBrowser.html?image=${image.id}")
25                         }
26                         webInterface.core.deleteImage(image)
27                         throw RedirectException("imageBrowser.html?album=${image.album.id}")
28                 }
29                 val image = webInterface.core.getImage(freenetRequest.httpRequest.getParam("image")) ?: throw RedirectException("invalid.html")
30                 if (!image.sone.isLocal) {
31                         throw RedirectException("noPermission.html")
32                 }
33                 templateContext["image"] = image
34         }
35
36 }