2 * Sone - UploadImagePage.java - Copyright © 2011 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 java.awt.Image;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
26 import javax.imageio.ImageIO;
28 import net.pterodactylus.sone.data.Album;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.web.page.Page.Request.Method;
31 import net.pterodactylus.util.io.Closer;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.template.Template;
34 import net.pterodactylus.util.template.TemplateContext;
35 import freenet.support.api.Bucket;
36 import freenet.support.api.HTTPUploadedFile;
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class UploadImagePage extends SoneTemplatePage {
46 private static final Logger logger = Logging.getLogger(UploadImagePage.class);
54 public UploadImagePage(Template template, WebInterface webInterface) {
55 super("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true);
59 // SONETEMPLATEPAGE METHODS
66 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
67 super.processTemplate(request, templateContext);
68 if (request.getMethod() == Method.POST) {
69 Sone currentSone = getCurrentSone(request.getToadletContext());
70 String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
71 Album parent = webInterface.getCore().getAlbum(parentId, false);
75 String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
76 String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000);
77 HTTPUploadedFile uploadedFile = request.getHttpRequest().getUploadedFile("image");
78 Bucket fileBucket = uploadedFile.getData();
79 InputStream imageInputStream = null;
80 net.pterodactylus.sone.data.Image image = null;
82 imageInputStream = fileBucket.getInputStream();
83 Image uploadedImage = ImageIO.read(imageInputStream);
84 if (uploadedImage == null) {
85 templateContext.set("messages", webInterface.getL10n().getString("Page.UploadImage.Error.InvalidImage"));
88 image = new net.pterodactylus.sone.data.Image().setSone(currentSone);
89 image.setTitle(name).setDescription(description).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null));
90 parent.addImage(image);
91 uploadedImage.flush();
92 } catch (IOException ioe1) {
93 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
96 Closer.close(imageInputStream);
99 throw new RedirectException("imageBrowser.html?image=" + image.getId());