Use method from JsonPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / GetImagePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.web.WebInterface
4 import net.pterodactylus.sone.web.page.FreenetPage
5 import net.pterodactylus.sone.web.page.FreenetRequest
6 import net.pterodactylus.util.web.Response
7 import java.net.URI
8
9 /**
10  * Page that delivers a {@link TemporaryImage} to the browser.
11  */
12 class GetImagePage(webInterface: WebInterface): FreenetPage {
13
14         private val core = webInterface.core
15
16         override fun getPath(): String {
17                 return "getImage.html"
18         }
19
20         override fun isPrefixPage(): Boolean {
21                 return false
22         }
23
24         override fun handleRequest(request: FreenetRequest, response: Response): Response {
25                 val image = core.getTemporaryImage(request.httpRequest.getParam("image")) ?: return response.apply {
26                         statusCode = 404
27                         statusText = "Not found."
28                         contentType = "text/html; charset=utf-8"
29                 }
30                 return response.apply {
31                         statusCode = 200
32                         contentType = image.mimeType
33                         content.write(image.imageData)
34                         addHeader("Content-Disposition", "attachment; filename=${image.id}.${image.mimeType.split('/')[1]}")
35                 }
36         }
37
38         override fun isLinkExcepted(link: URI?): Boolean {
39                 return false
40         }
41
42 }