Replace mark as known ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / UploadImagePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import freenet.support.api.Bucket
4 import net.pterodactylus.sone.text.TextFilter
5 import net.pterodactylus.sone.utils.emptyToNull
6 import net.pterodactylus.sone.utils.headers
7 import net.pterodactylus.sone.utils.isPOST
8 import net.pterodactylus.sone.utils.parameters
9 import net.pterodactylus.sone.utils.use
10 import net.pterodactylus.sone.web.WebInterface
11 import net.pterodactylus.sone.web.page.FreenetRequest
12 import net.pterodactylus.util.template.Template
13 import net.pterodactylus.util.template.TemplateContext
14 import org.bouncycastle.asn1.x500.style.RFC4519Style.title
15 import java.awt.image.BufferedImage
16 import java.io.ByteArrayInputStream
17 import java.io.ByteArrayOutputStream
18 import javax.imageio.ImageIO
19
20 /**
21  * Page implementation that lets the user upload an image.
22  */
23 class UploadImagePage(template: Template, webInterface: WebInterface):
24                 SoneTemplatePage("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true) {
25
26         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
27                 if (request.isPOST) {
28                         val parentAlbum = request.parameters["parent"]!!.let(webInterface.core::getAlbum) ?: throw RedirectException("noPermission.html")
29                         if (parentAlbum.sone != getCurrentSone(request.toadletContext)) {
30                                 throw RedirectException("noPermission.html")
31                         }
32                         val title = request.parameters["title", 200].emptyToNull ?: throw RedirectException("emptyImageTitle.html")
33
34                         val uploadedFile = request.httpRequest.getUploadedFile("image")
35                         val bytes = uploadedFile.data.use { it.toByteArray() }
36                         val bufferedImage = bytes.toImage()
37                         if (bufferedImage == null) {
38                                 templateContext["messages"] = webInterface.l10n.getString("Page.UploadImage.Error.InvalidImage")
39                                 return
40                         }
41
42                         val temporaryImage = webInterface.core.createTemporaryImage(bytes.mimeType, bytes)
43                         webInterface.core.createImage(getCurrentSone(request.toadletContext), parentAlbum, temporaryImage).modify().apply {
44                                 setWidth(bufferedImage.width)
45                                 setHeight(bufferedImage.height)
46                                 setTitle(title)
47                                 setDescription(TextFilter.filter(request.headers["Host"], request.parameters["description", 4000]))
48                         }.update()
49                         throw RedirectException("imageBrowser.html?album=${parentAlbum.id}")
50                 }
51         }
52
53         private fun Bucket.toByteArray(): ByteArray = ByteArrayOutputStream(size().toInt()).use { outputStream ->
54                 inputStream.copyTo(outputStream)
55                 outputStream.toByteArray()
56         }
57
58         private fun ByteArray.toImage(): BufferedImage? = ByteArrayInputStream(this).use {
59                 ImageIO.read(it)
60         }
61
62         private val ByteArray.mimeType get() = ByteArrayInputStream(this).use {
63                 ImageIO.createImageInputStream(it).use {
64                         ImageIO.getImageReaders(it).asSequence()
65                                         .firstOrNull()?.originatingProvider?.mimeTypes?.firstOrNull()
66                                         ?: UNKNOWN_MIME_TYPE
67                 }
68         }
69
70 }
71
72 private const val UNKNOWN_MIME_TYPE = "application/octet-stream"