+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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;
- }
-
-}
--- /dev/null
+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
+ }
+
+}
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
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)))
}
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")
+ ))
}
}