Prevent NPE when creating a new Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / UploadImagePage.java
1 /*
2  * Sone - UploadImagePage.java - Copyright © 2011–2013 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.awt.Image;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Iterator;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import javax.imageio.ImageIO;
30 import javax.imageio.ImageReader;
31 import javax.imageio.stream.ImageInputStream;
32
33 import net.pterodactylus.sone.data.Album;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.data.TemporaryImage;
36 import net.pterodactylus.sone.text.TextFilter;
37 import net.pterodactylus.sone.web.page.FreenetRequest;
38 import net.pterodactylus.util.io.Closer;
39 import net.pterodactylus.util.logging.Logging;
40 import net.pterodactylus.util.template.Template;
41 import net.pterodactylus.util.template.TemplateContext;
42 import net.pterodactylus.util.web.Method;
43
44 import com.google.common.io.ByteStreams;
45
46 import freenet.support.api.Bucket;
47 import freenet.support.api.HTTPUploadedFile;
48
49 /**
50  * Page implementation that lets the user upload an image.
51  *
52  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53  */
54 public class UploadImagePage extends SoneTemplatePage {
55
56         /** The logger. */
57         private static final Logger logger = Logging.getLogger(UploadImagePage.class);
58
59         /**
60          * Creates a new “upload image” page.
61          *
62          * @param template
63          *            The template to render
64          * @param webInterface
65          *            The Sone web interface
66          */
67         public UploadImagePage(Template template, WebInterface webInterface) {
68                 super("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true);
69         }
70
71         //
72         // SONETEMPLATEPAGE METHODS
73         //
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
80                 super.processTemplate(request, templateContext);
81                 if (request.getMethod() == Method.POST) {
82                         Sone currentSone = getCurrentSone(request.getToadletContext());
83                         String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
84                         Album parent = webInterface.getCore().getAlbum(parentId, false);
85                         if (parent == null) {
86                                 /* TODO - signal error */
87                                 return;
88                         }
89                         if (!currentSone.equals(parent.getSone())) {
90                                 /* TODO - signal error. */
91                                 return;
92                         }
93                         String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
94                         String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000);
95                         HTTPUploadedFile uploadedFile = request.getHttpRequest().getUploadedFile("image");
96                         Bucket fileBucket = uploadedFile.getData();
97                         InputStream imageInputStream = null;
98                         ByteArrayOutputStream imageDataOutputStream = null;
99                         net.pterodactylus.sone.data.Image image = null;
100                         try {
101                                 imageInputStream = fileBucket.getInputStream();
102                                 /* TODO - check length */
103                                 imageDataOutputStream = new ByteArrayOutputStream((int) fileBucket.size());
104                                 ByteStreams.copy(imageInputStream, imageDataOutputStream);
105                         } catch (IOException ioe1) {
106                                 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
107                                 return;
108                         } finally {
109                                 fileBucket.free();
110                                 Closer.close(imageInputStream);
111                                 Closer.close(imageDataOutputStream);
112                         }
113                         byte[] imageData = imageDataOutputStream.toByteArray();
114                         ByteArrayInputStream imageDataInputStream = null;
115                         Image uploadedImage = null;
116                         try {
117                                 imageDataInputStream = new ByteArrayInputStream(imageData);
118                                 uploadedImage = ImageIO.read(imageDataInputStream);
119                                 if (uploadedImage == null) {
120                                         templateContext.set("messages", webInterface.getL10n().getString("Page.UploadImage.Error.InvalidImage"));
121                                         return;
122                                 }
123                                 String mimeType = getMimeType(imageData);
124                                 TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData);
125                                 image = webInterface.getCore().createImage(currentSone, parent, temporaryImage);
126                                 image.setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null));
127                         } catch (IOException ioe1) {
128                                 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
129                                 return;
130                         } finally {
131                                 Closer.close(imageDataInputStream);
132                                 Closer.flush(uploadedImage);
133                         }
134                         throw new RedirectException("imageBrowser.html?album=" + parent.getId());
135                 }
136         }
137
138         //
139         // PRIVATE METHODS
140         //
141
142         /**
143          * Tries to detect the MIME type of the encoded image.
144          *
145          * @param imageData
146          *            The encoded image
147          * @return The MIME type of the image, or “application/octet-stream” if the
148          *         image type could not be detected
149          */
150         private static String getMimeType(byte[] imageData) {
151                 ByteArrayInputStream imageDataInputStream = new ByteArrayInputStream(imageData);
152                 try {
153                         ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream);
154                         Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
155                         if (imageReaders.hasNext()) {
156                                 return imageReaders.next().getOriginatingProvider().getMIMETypes()[0];
157                         }
158                 } catch (IOException ioe1) {
159                         logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1);
160                 }
161                 return "application/octet-stream";
162         }
163
164 }