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