Replace upload image page with Kotlin version
[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.text.TextFilter
5 import net.pterodactylus.sone.utils.isPOST
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * Page that lets the user edit title and description of an {@link Image}.
13  */
14 class EditImagePage(template: Template, webInterface: WebInterface):
15                 SoneTemplatePage("editImage.html", template, "Page.EditImage.Title", webInterface, true) {
16
17         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
18                 if (request.isPOST) {
19                         val image = webInterface.core.getImage(request.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html")
20                         if (!image.sone.isLocal) {
21                                 throw RedirectException("noPermission.html")
22                         }
23                         request.httpRequest.getPartAsStringFailsafe("returnPage", 256).let { returnPage ->
24                                 if (request.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
25                                         image.album.moveImageUp(image)
26                                         webInterface.core.touchConfiguration()
27                                 } else if (request.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
28                                         image.album.moveImageDown(image)
29                                         webInterface.core.touchConfiguration()
30                                 } else {
31                                         try {
32                                                 image.modify()
33                                                                 .setTitle(request.httpRequest.getPartAsStringFailsafe("title", 100))
34                                                                 .setDescription(TextFilter.filter(request.httpRequest.getHeader("Host"), request.httpRequest.getPartAsStringFailsafe("description", 1024)))
35                                                                 .update()
36                                                 webInterface.core.touchConfiguration()
37                                         } catch (e: ImageTitleMustNotBeEmpty) {
38                                                 throw RedirectException("emptyImageTitle.html")
39                                         }
40                                 }
41                                 throw RedirectException(returnPage)
42                         }
43                 }
44         }
45
46 }