2 * Sone - GetImagePage.java - Copyright © 2011–2016 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web.pages;
20 import java.io.IOException;
23 import net.pterodactylus.sone.data.TemporaryImage;
24 import net.pterodactylus.sone.web.WebInterface;
25 import net.pterodactylus.sone.web.page.FreenetPage;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import net.pterodactylus.util.web.Response;
30 * Page that delivers a {@link TemporaryImage} to the browser.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class GetImagePage implements FreenetPage {
36 /** The Sone web interface. */
37 private final WebInterface webInterface;
40 * Creates a new “get image” page.
43 * The Sone web interface
45 public GetImagePage(WebInterface webInterface) {
46 this.webInterface = webInterface;
53 public String getPath() {
54 return "getImage.html";
61 public boolean isPrefixPage() {
69 public Response handleRequest(FreenetRequest request, Response response) throws IOException {
70 String imageId = request.getHttpRequest().getParam("image");
71 TemporaryImage temporaryImage = webInterface.getCore().getTemporaryImage(imageId);
72 if (temporaryImage == null) {
73 return response.setStatusCode(404).setStatusText("Not found.").setContentType("text/html; charset=utf-8");
75 String contentType= temporaryImage.getMimeType();
76 return response.setStatusCode(200).setStatusText("OK").setContentType(contentType).addHeader("Content-Disposition", "attachment; filename=" + temporaryImage.getId() + "." + contentType.substring(contentType.lastIndexOf('/') + 1)).write(temporaryImage.getImageData());
83 public boolean isLinkExcepted(URI link) {