1 package net.pterodactylus.sone.web.pages
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
21 * Page implementation that lets the user upload an image.
23 class UploadImagePage(template: Template, webInterface: WebInterface):
24 SoneTemplatePage("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true) {
26 override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
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")
32 val title = request.parameters["title", 200].emptyToNull ?: throw RedirectException("emptyImageTitle.html")
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")
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)
47 setDescription(TextFilter.filter(request.headers["Host"], request.parameters["description", 4000]))
49 throw RedirectException("imageBrowser.html?album=${parentAlbum.id}")
53 private fun Bucket.toByteArray(): ByteArray = ByteArrayOutputStream(size().toInt()).use { outputStream ->
54 inputStream.copyTo(outputStream)
55 outputStream.toByteArray()
58 private fun ByteArray.toImage(): BufferedImage? = ByteArrayInputStream(this).use {
62 private val ByteArray.mimeType get() = ByteArrayInputStream(this).use {
63 ImageIO.createImageInputStream(it).use {
64 ImageIO.getImageReaders(it).asSequence()
65 .firstOrNull()?.originatingProvider?.mimeTypes?.firstOrNull()
72 private const val UNKNOWN_MIME_TYPE = "application/octet-stream"