From e9e8f21fdfe533e8946a64d53dabaf48c3c296c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 27 Apr 2017 22:11:18 +0200 Subject: [PATCH] Replace get image page with Kotlin version --- .../pterodactylus/sone/web/pages/GetImagePage.java | 87 ---------------------- .../pterodactylus/sone/web/pages/GetImagePage.kt | 42 +++++++++++ .../sone/web/pages/GetImagePageTest.kt | 9 ++- 3 files changed, 49 insertions(+), 89 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java b/src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java deleted file mode 100644 index bcf0eda..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Sone - GetImagePage.java - Copyright © 2011–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.pages; - -import java.io.IOException; -import java.net.URI; - -import net.pterodactylus.sone.data.TemporaryImage; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetPage; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.web.Response; - -/** - * Page that delivers a {@link TemporaryImage} to the browser. - * - * @author David ‘Bombe’ Roden - */ -public class GetImagePage implements FreenetPage { - - /** The Sone web interface. */ - private final WebInterface webInterface; - - /** - * Creates a new “get image” page. - * - * @param webInterface - * The Sone web interface - */ - public GetImagePage(WebInterface webInterface) { - this.webInterface = webInterface; - } - - /** - * {@inheritDoc} - */ - @Override - public String getPath() { - return "getImage.html"; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isPrefixPage() { - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public Response handleRequest(FreenetRequest request, Response response) throws IOException { - String imageId = request.getHttpRequest().getParam("image"); - TemporaryImage temporaryImage = webInterface.getCore().getTemporaryImage(imageId); - if (temporaryImage == null) { - return response.setStatusCode(404).setStatusText("Not found.").setContentType("text/html; charset=utf-8"); - } - String contentType= temporaryImage.getMimeType(); - return response.setStatusCode(200).setStatusText("OK").setContentType(contentType).addHeader("Content-Disposition", "attachment; filename=" + temporaryImage.getId() + "." + contentType.substring(contentType.lastIndexOf('/') + 1)).write(temporaryImage.getImageData()); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isLinkExcepted(URI link) { - return false; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt new file mode 100644 index 0000000..3e9eed0 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt @@ -0,0 +1,42 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetPage +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.web.Response +import java.net.URI + +/** + * Page that delivers a {@link TemporaryImage} to the browser. + */ +class GetImagePage(webInterface: WebInterface): FreenetPage { + + private val core = webInterface.core!! + + override fun getPath(): String { + return "getImage.html" + } + + override fun isPrefixPage(): Boolean { + return false + } + + override fun handleRequest(request: FreenetRequest, response: Response): Response { + val image = core.getTemporaryImage(request.httpRequest.getParam("image")) ?: return response.apply { + statusCode = 404 + statusText = "Not found." + contentType = "text/html; charset=utf-8" + } + return response.apply { + statusCode = 200 + contentType = image.mimeType + content.write(image.imageData) + addHeader("Content-Disposition", "attachment; filename=${image.id}.${image.mimeType.split('/')[1]}") + } + } + + override fun isLinkExcepted(link: URI?): Boolean { + return false + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt index fb96a4c..151f1ae 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt @@ -1,9 +1,8 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.TemporaryImage -import net.pterodactylus.sone.web.pages.GetImagePage -import net.pterodactylus.sone.web.pages.WebPageTest import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo import org.junit.Test @@ -33,6 +32,8 @@ class GetImagePageTest : WebPageTest() { fun `invalid image returns 404 response`() { page.handleRequest(freenetRequest, response) assertThat(response.statusCode, equalTo(404)) + assertThat(response.statusText, equalTo("Not found.")) + assertThat(response.contentType, equalTo("text/html; charset=utf-8")) assertThat(responseBytes, equalTo(ByteArray(0))) } @@ -48,6 +49,10 @@ class GetImagePageTest : WebPageTest() { assertThat(response.statusCode, equalTo(200)) assertThat(response.contentType, equalTo("image/test")) assertThat(responseBytes, equalTo(ByteArray(5, Int::toByte))) + println(response.headers.map { it.name to it.iterator().asSequence().toList() }) + assertThat(response.headers.map { it.name to it.iterator().asSequence().toList() }, contains( + "Content-Disposition" to listOf("attachment; filename=temp-id.test") + )) } } -- 2.7.4