Add test for DI constructability of DistrustPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditImagePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.text.TextFilter
6 import net.pterodactylus.sone.utils.isPOST
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11
12 /**
13  * Page that lets the user edit title and description of an {@link Image}.
14  */
15 class EditImagePage(template: Template, webInterface: WebInterface):
16                 LoggedInPage("editImage.html", template, "Page.EditImage.Title", webInterface) {
17
18         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 if (freenetRequest.isPOST) {
20                         val image = webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
21                         if (!image.sone.isLocal) {
22                                 throw RedirectException("noPermission.html")
23                         }
24                         freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256).let { returnPage ->
25                                 if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
26                                         image.album.moveImageUp(image)
27                                         webInterface.core.touchConfiguration()
28                                 } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
29                                         image.album.moveImageDown(image)
30                                         webInterface.core.touchConfiguration()
31                                 } else {
32                                         try {
33                                                 image.modify()
34                                                                 .setTitle(freenetRequest.httpRequest.getPartAsStringFailsafe("title", 100))
35                                                                 .setDescription(TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), freenetRequest.httpRequest.getPartAsStringFailsafe("description", 1024)))
36                                                                 .update()
37                                                 webInterface.core.touchConfiguration()
38                                         } catch (e: ImageTitleMustNotBeEmpty) {
39                                                 throw RedirectException("emptyImageTitle.html")
40                                         }
41                                 }
42                                 throw RedirectException(returnPage)
43                         }
44                 }
45         }
46
47 }