Add basic image uploading capability.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / UploadImagePage.java
1 /*
2  * Sone - UploadImagePage.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.awt.Image;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import javax.imageio.ImageIO;
27
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;
37
38 /**
39  * TODO
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class UploadImagePage extends SoneTemplatePage {
44
45         /** The logger. */
46         private static final Logger logger = Logging.getLogger(UploadImagePage.class);
47
48         /**
49          * TODO
50          *
51          * @param template
52          * @param webInterface
53          */
54         public UploadImagePage(Template template, WebInterface webInterface) {
55                 super("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true);
56         }
57
58         //
59         // SONETEMPLATEPAGE METHODS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
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);
72                         if (parent == null) {
73                                 throw new RedirectException("invalid.html");
74                         }
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;
81                         try {
82                                 imageInputStream = fileBucket.getInputStream();
83                                 Image uploadedImage = ImageIO.read(imageInputStream);
84                                 if (uploadedImage == null) {
85                                         throw new RedirectException("invalid.html");
86                                 }
87                                 image = new net.pterodactylus.sone.data.Image().setSone(currentSone);
88                                 image.setTitle(name).setDescription(description).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null));
89                                 parent.addImage(image);
90                                 uploadedImage.flush();
91                         } catch (IOException ioe1) {
92                                 logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
93                                 throw new RedirectException("invalid.html");
94                         } finally {
95                                 Closer.close(imageInputStream);
96                                 fileBucket.free();
97                         }
98                         throw new RedirectException("imageBrowser.html?image=" + image.getId());
99                 }
100         }
101
102 }