From: David ‘Bombe’ Roden Date: Sun, 10 Apr 2011 18:16:25 +0000 (+0200) Subject: Add page that delivers the encoded data of a temporary image. X-Git-Tag: beta-freefall-0.6.2-1~46 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=456a0d35f3afcdaf4c2f82048f8cb440059f70e7 Add page that delivers the encoded data of a temporary image. --- diff --git a/src/main/java/net/pterodactylus/sone/web/GetImagePage.java b/src/main/java/net/pterodactylus/sone/web/GetImagePage.java new file mode 100644 index 0000000..fdd72a8 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/GetImagePage.java @@ -0,0 +1,64 @@ +/* + * Sone - GetImagePage.java - Copyright © 2011 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; + +import net.pterodactylus.sone.data.TemporaryImage; +import net.pterodactylus.sone.web.page.Page; + +/** + * Page that delivers a {@link TemporaryImage} to the browser. + * + * @author David ‘Bombe’ Roden + */ +public class GetImagePage implements Page { + + /** 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 Response handleRequest(Request request) { + String imageId = request.getHttpRequest().getParam("image"); + TemporaryImage temporaryImage = webInterface.getCore().getTemporaryImage(imageId); + if (temporaryImage == null) { + return new Response(404, "Not found.", "text/plain; charset=utf-8", ""); + } + return new Response(200, "OK", temporaryImage.getMimeType(), temporaryImage.getImageData()).setHeader("Content-Disposition", "attachment; filename=" + temporaryImage.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1)); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 5a16f8c..5b1ae6c 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -583,6 +583,8 @@ public class WebInterface implements CoreListener { pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png"))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));