🚧 Change rescheduling method
[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.*
4 import net.pterodactylus.sone.data.*
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 java.awt.image.*
12 import java.io.*
13 import javax.imageio.*
14 import javax.inject.*
15
16 /**
17  * Page implementation that lets the user upload an image.
18  */
19 @TemplatePath("/templates/invalid.html")
20 @ToadletPath("uploadImage.html")
21 class UploadImagePage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
22                 LoggedInPage("Page.UploadImage.Title", webInterface, loaders, templateRenderer) {
23
24         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
25                 if (soneRequest.isPOST) {
26                         val parentAlbum = soneRequest.parameters["parent"]!!.let(soneRequest.core::getAlbum) ?: redirectTo("noPermission.html")
27                         if (parentAlbum.sone != currentSone) {
28                                 redirectTo("noPermission.html")
29                         }
30                         val title = soneRequest.parameters["title", 200].emptyToNull ?: redirectTo("emptyImageTitle.html")
31
32                         val uploadedFile = soneRequest.httpRequest.getUploadedFile("image")
33                         val bytes = uploadedFile.data.use { it.toByteArray() }
34                         val bufferedImage = bytes.toImage()
35                         if (bufferedImage == null) {
36                                 templateContext["messages"] = translation.translate("Page.UploadImage.Error.InvalidImage")
37                                 return
38                         }
39
40                         val temporaryImage = soneRequest.core.createTemporaryImage(bytes.mimeType, bytes)
41                         soneRequest.core.createImage(currentSone, parentAlbum, temporaryImage).modify().apply {
42                                 setWidth(bufferedImage.width)
43                                 setHeight(bufferedImage.height)
44                                 setTitle(title)
45                                 setDescription(TextFilter.filter(soneRequest.headers["Host"], soneRequest.parameters["description", 4000]))
46                         }.update()
47                         redirectTo("imageBrowser.html?album=${parentAlbum.id}")
48                 }
49         }
50
51         private fun Bucket.toByteArray(): ByteArray = ByteArrayOutputStream(size().toInt()).use { outputStream ->
52                 inputStream.copyTo(outputStream)
53                 outputStream.toByteArray()
54         }
55
56         private fun ByteArray.toImage(): BufferedImage? = ByteArrayInputStream(this).use {
57                 ImageIO.read(it)
58         }
59
60         private val ByteArray.mimeType
61                 get() = ByteArrayInputStream(this).use {
62                         ImageIO.createImageInputStream(it).use {
63                                 ImageIO.getImageReaders(it).asSequence()
64                                                 .firstOrNull()?.originatingProvider?.mimeTypes?.firstOrNull()
65                                                 ?: UNKNOWN_MIME_TYPE
66                         }
67                 }
68
69 }
70
71 private const val UNKNOWN_MIME_TYPE = "application/octet-stream"