Add basic image uploading capability.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Mar 2011 19:58:20 +0000 (20:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Mar 2011 19:58:20 +0000 (20:58 +0100)
src/main/java/net/pterodactylus/sone/web/UploadImagePage.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/imageBrowser.html
src/main/resources/templates/include/uploadImage.html [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java
new file mode 100644 (file)
index 0000000..2b25263
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Sone - UploadImagePage.java - Copyright © 2011 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web;
+
+import java.awt.Image;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.imageio.ImageIO;
+
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.web.page.Page.Request.Method;
+import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import freenet.support.api.Bucket;
+import freenet.support.api.HTTPUploadedFile;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class UploadImagePage extends SoneTemplatePage {
+
+       /** The logger. */
+       private static final Logger logger = Logging.getLogger(UploadImagePage.class);
+
+       /**
+        * TODO
+        *
+        * @param template
+        * @param webInterface
+        */
+       public UploadImagePage(Template template, WebInterface webInterface) {
+               super("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true);
+       }
+
+       //
+       // SONETEMPLATEPAGE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
+               super.processTemplate(request, templateContext);
+               if (request.getMethod() == Method.POST) {
+                       Sone currentSone = getCurrentSone(request.getToadletContext());
+                       String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
+                       Album parent = webInterface.getCore().getAlbum(parentId, false);
+                       if (parent == null) {
+                               throw new RedirectException("invalid.html");
+                       }
+                       String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
+                       String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000);
+                       HTTPUploadedFile uploadedFile = request.getHttpRequest().getUploadedFile("image");
+                       Bucket fileBucket = uploadedFile.getData();
+                       InputStream imageInputStream = null;
+                       net.pterodactylus.sone.data.Image image = null;
+                       try {
+                               imageInputStream = fileBucket.getInputStream();
+                               Image uploadedImage = ImageIO.read(imageInputStream);
+                               if (uploadedImage == null) {
+                                       throw new RedirectException("invalid.html");
+                               }
+                               image = new net.pterodactylus.sone.data.Image().setSone(currentSone);
+                               image.setTitle(name).setDescription(description).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null));
+                               parent.addImage(image);
+                               uploadedImage.flush();
+                       } catch (IOException ioe1) {
+                               logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
+                               throw new RedirectException("invalid.html");
+                       } finally {
+                               Closer.close(imageInputStream);
+                               fileBucket.free();
+                       }
+                       throw new RedirectException("imageBrowser.html?image=" + image.getId());
+               }
+       }
+
+}
index e3a95d4..09e3dcd 100644 (file)
@@ -564,6 +564,7 @@ public class WebInterface implements CoreListener {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(emptyTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
index b1c4243..4db1a07 100644 (file)
@@ -167,6 +167,8 @@ Page.CreateAlbum.Title=Create Album - Sone
 Page.CreateAlbum.Page.Title=Create Album
 Page.CreateAlbum.Error.NameMissing=You seem to have forgotten to enter a name for your new album.
 
+Page.UploadImage.Title=Upload Image - Sone
+
 Page.Trust.Title=Trust Sone - Sone
 
 Page.Distrust.Title=Distrust Sone - Sone
@@ -238,6 +240,11 @@ View.CreateAlbum.Title=Create Album
 View.CreateAlbum.Label.Name=Name:
 View.CreateAlbum.Label.Description=Description:
 
+View.UploadImage.Title=Upload Image
+View.UploadImage.Label.Title=Title:
+View.UploadImage.Label.Description=Description:
+View.UploadImage.Button.UploadImage=Upload Image
+
 WebInterface.DefaultText.StatusUpdate=What’s on your mind?
 WebInterface.DefaultText.Message=Write a Message…
 WebInterface.DefaultText.Reply=Write a Reply…
index c2257bd..53126a8 100644 (file)
@@ -65,6 +65,7 @@
                        </div>
 
                        <%if album.sone.current>
+                               <%include include/uploadImage.html>
                                <%include include/createAlbum.html>
                        <%/if>
 
diff --git a/src/main/resources/templates/include/uploadImage.html b/src/main/resources/templates/include/uploadImage.html
new file mode 100644 (file)
index 0000000..45eb5f2
--- /dev/null
@@ -0,0 +1,12 @@
+<h2><%= View.UploadImage.Title|l10n|html></h2>
+
+<form id="upload-image" method="post" action="uploadImage.html" enctype="multipart/form-data">
+       <input type="hidden" name="formPassword" value="<% formPassword|html>" />
+       <input type="hidden" name="parent" value="<% album.id|html>" />
+       <label for="title"><%= View.UploadImage.Label.Title|l10n|html></label>
+       <input type="text" name="title" value="" />
+       <label for="description"><%= View.UploadImage.Label.Description|l10n|html></label>
+       <input type="text" name="description" value="" />
+       <input type="file" name="image" />
+       <button type="submit" name="uploadImage" value="1"><%= View.UploadImage.Button.UploadImage|l10n|html></button>
+</form>