Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / GetImagePage.java
1 /*
2  * Sone - GetImagePage.java - Copyright © 2011–2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.io.IOException;
21 import java.net.URI;
22
23 import net.pterodactylus.sone.data.TemporaryImage;
24 import net.pterodactylus.sone.web.page.FreenetPage;
25 import net.pterodactylus.sone.web.page.FreenetRequest;
26 import net.pterodactylus.util.web.Response;
27
28 /**
29  * Page that delivers a {@link TemporaryImage} to the browser.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class GetImagePage implements FreenetPage {
34
35         /** The Sone web interface. */
36         private final WebInterface webInterface;
37
38         /**
39          * Creates a new “get image” page.
40          *
41          * @param webInterface
42          *            The Sone web interface
43          */
44         public GetImagePage(WebInterface webInterface) {
45                 this.webInterface = webInterface;
46         }
47
48         /**
49          * {@inheritDoc}
50          */
51         @Override
52         public String getPath() {
53                 return "getImage.html";
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
60         public boolean isPrefixPage() {
61                 return false;
62         }
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         public Response handleRequest(FreenetRequest request, Response response) throws IOException {
69                 String imageId = request.getHttpRequest().getParam("image");
70                 TemporaryImage temporaryImage = webInterface.getCore().getTemporaryImage(imageId);
71                 if (temporaryImage == null) {
72                         return response.setStatusCode(404).setStatusText("Not found.").setContentType("text/html; charset=utf-8");
73                 }
74                 String contentType= temporaryImage.getMimeType();
75                 return response.setStatusCode(200).setStatusText("OK").setContentType(contentType).addHeader("Content-Disposition", "attachment; filename=" + temporaryImage.getId() + "." + contentType.substring(contentType.lastIndexOf('/') + 1)).write(temporaryImage.getImageData());
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         public boolean isLinkExcepted(URI link) {
83                 return false;
84         }
85
86 }