2 * Sone - UploadImagePage.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;
20 import static com.google.common.base.Optional.fromNullable;
21 import static java.util.logging.Logger.getLogger;
23 import java.awt.Image;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Iterator;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
32 import javax.imageio.ImageIO;
33 import javax.imageio.ImageReader;
34 import javax.imageio.stream.ImageInputStream;
36 import net.pterodactylus.sone.data.Album;
37 import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.data.TemporaryImage;
40 import net.pterodactylus.sone.text.TextFilter;
41 import net.pterodactylus.sone.web.page.FreenetRequest;
42 import net.pterodactylus.util.io.Closer;
43 import net.pterodactylus.util.template.Template;
44 import net.pterodactylus.util.template.TemplateContext;
45 import net.pterodactylus.util.web.Method;
47 import com.google.common.io.ByteStreams;
49 import freenet.support.api.Bucket;
50 import freenet.support.api.HTTPUploadedFile;
53 * Page implementation that lets the user upload an image.
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class UploadImagePage extends SoneTemplatePage {
59 private static final Logger logger = getLogger(UploadImagePage.class.getName());
60 private static final String UNKNOWN_MIME_TYPE = "application/octet-stream";
63 * Creates a new “upload image” page.
66 * The template to render
68 * The Sone web interface
70 public UploadImagePage(Template template, WebInterface webInterface) {
71 super("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true);
75 // SONETEMPLATEPAGE METHODS
82 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
83 super.processTemplate(request, templateContext);
84 if (request.getMethod() == Method.POST) {
85 Sone currentSone = getCurrentSone(request.getToadletContext());
86 String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
87 Album parent = webInterface.getCore().getAlbum(parentId);
89 throw new RedirectException("noPermission.html");
91 if (!currentSone.equals(parent.getSone())) {
92 throw new RedirectException("noPermission.html");
94 String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200).trim();
95 if (name.length() == 0) {
96 throw new RedirectException("emptyImageTitle.html");
98 String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000);
99 HTTPUploadedFile uploadedFile = request.getHttpRequest().getUploadedFile("image");
100 Bucket fileBucket = uploadedFile.getData();
101 InputStream imageInputStream = null;
102 ByteArrayOutputStream imageDataOutputStream = null;
104 imageInputStream = fileBucket.getInputStream();
105 /* TODO - check length */
106 imageDataOutputStream = new ByteArrayOutputStream((int) fileBucket.size());
107 ByteStreams.copy(imageInputStream, imageDataOutputStream);
108 } catch (IOException ioe1) {
109 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
113 Closer.close(imageInputStream);
114 Closer.close(imageDataOutputStream);
116 byte[] imageData = imageDataOutputStream.toByteArray();
117 ByteArrayInputStream imageDataInputStream = null;
118 Image uploadedImage = null;
120 imageDataInputStream = new ByteArrayInputStream(imageData);
121 uploadedImage = ImageIO.read(imageDataInputStream);
122 if (uploadedImage == null) {
123 templateContext.set("messages", webInterface.getL10n().getString("Page.UploadImage.Error.InvalidImage"));
126 String mimeType = getMimeType(imageData);
127 TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData);
128 net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent, temporaryImage);
129 image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update();
130 } catch (IOException ioe1) {
131 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
133 } catch (ImageTitleMustNotBeEmpty itmnbe) {
134 throw new RedirectException("emptyImageTitle.html");
136 Closer.close(imageDataInputStream);
137 Closer.flush(uploadedImage);
139 throw new RedirectException("imageBrowser.html?album=" + parent.getId());
148 * Tries to detect the MIME type of the encoded image.
152 * @return The MIME type of the image, or “application/octet-stream” if the
153 * image type could not be detected
155 private static String getMimeType(byte[] imageData) {
156 ByteArrayInputStream imageDataInputStream = new ByteArrayInputStream(imageData);
158 ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream);
159 Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
160 if (imageReaders.hasNext()) {
161 return fromNullable(imageReaders.next().getOriginatingProvider().getMIMETypes())
162 .or(new String[] { UNKNOWN_MIME_TYPE })[0];
164 } catch (IOException ioe1) {
165 logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1);
167 return UNKNOWN_MIME_TYPE;