🚧 Change rescheduling method
[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.*
4 import net.pterodactylus.sone.data.Image.Modifier.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.text.*
7 import net.pterodactylus.sone.utils.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.template.*
11 import javax.inject.*
12
13 /**
14  * Page that lets the user edit title and description of an {@link Image}.
15  */
16 @ToadletPath("editImage.html")
17 class EditImagePage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
18                 LoggedInPage("Page.EditImage.Title", webInterface, loaders, templateRenderer) {
19
20         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
21                 if (soneRequest.isPOST) {
22                         val image = soneRequest.core.getImage(soneRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: redirectTo("invalid.html")
23                         if (!image.sone.isLocal) {
24                                 redirectTo("noPermission.html")
25                         }
26                         soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256).let { returnPage ->
27                                 if (soneRequest.httpRequest.getPartAsStringFailsafe("moveLeft", 4) == "true") {
28                                         image.album.moveImageUp(image)
29                                         soneRequest.core.touchConfiguration()
30                                 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("moveRight", 4) == "true") {
31                                         image.album.moveImageDown(image)
32                                         soneRequest.core.touchConfiguration()
33                                 } else {
34                                         try {
35                                                 image.modify()
36                                                                 .setTitle(soneRequest.httpRequest.getPartAsStringFailsafe("title", 100))
37                                                                 .setDescription(TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), soneRequest.httpRequest.getPartAsStringFailsafe("description", 1024)))
38                                                                 .update()
39                                                 soneRequest.core.touchConfiguration()
40                                         } catch (e: ImageTitleMustNotBeEmpty) {
41                                                 redirectTo("emptyImageTitle.html")
42                                         }
43                                 }
44                                 redirectTo(returnPage)
45                         }
46                 }
47         }
48
49 }