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