Add test for DI constructability of IndexPage
[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 import javax.inject.Inject
12
13 /**
14  * Page that lets the user edit title and description of an {@link Image}.
15  */
16 class EditImagePage @Inject constructor(template: Template, webInterface: WebInterface):
17                 LoggedInPage("editImage.html", template, "Page.EditImage.Title", webInterface) {
18
19         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 if (freenetRequest.isPOST) {
21                         val image = webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
22                         if (!image.sone.isLocal) {
23                                 throw RedirectException("noPermission.html")
24                         }
25                         freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256).let { returnPage ->
26                                 if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
27                                         image.album.moveImageUp(image)
28                                         webInterface.core.touchConfiguration()
29                                 } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
30                                         image.album.moveImageDown(image)
31                                         webInterface.core.touchConfiguration()
32                                 } else {
33                                         try {
34                                                 image.modify()
35                                                                 .setTitle(freenetRequest.httpRequest.getPartAsStringFailsafe("title", 100))
36                                                                 .setDescription(TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), freenetRequest.httpRequest.getPartAsStringFailsafe("description", 1024)))
37                                                                 .update()
38                                                 webInterface.core.touchConfiguration()
39                                         } catch (e: ImageTitleMustNotBeEmpty) {
40                                                 throw RedirectException("emptyImageTitle.html")
41                                         }
42                                 }
43                                 throw RedirectException(returnPage)
44                         }
45                 }
46         }
47
48 }