1 package net.pterodactylus.sone.web.pages
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.*
13 import javax.imageio.*
17 * Page implementation that lets the user upload an image.
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) {
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")
30 val title = soneRequest.parameters["title", 200].emptyToNull ?: redirectTo("emptyImageTitle.html")
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")
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)
45 setDescription(TextFilter.filter(soneRequest.headers["Host"], soneRequest.parameters["description", 4000]))
47 redirectTo("imageBrowser.html?album=${parentAlbum.id}")
51 private fun Bucket.toByteArray(): ByteArray = ByteArrayOutputStream(size().toInt()).use { outputStream ->
52 inputStream.copyTo(outputStream)
53 outputStream.toByteArray()
56 private fun ByteArray.toImage(): BufferedImage? = ByteArrayInputStream(this).use {
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()
71 private const val UNKNOWN_MIME_TYPE = "application/octet-stream"