From 77baeade882c7d936b7293a35303d824ae183994 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 23 Mar 2011 20:58:20 +0100 Subject: [PATCH] Add basic image uploading capability. --- .../pterodactylus/sone/web/UploadImagePage.java | 102 +++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 1 + src/main/resources/i18n/sone.en.properties | 7 ++ src/main/resources/templates/imageBrowser.html | 1 + .../resources/templates/include/uploadImage.html | 12 +++ 5 files changed, 123 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/web/UploadImagePage.java create mode 100644 src/main/resources/templates/include/uploadImage.html 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 index 0000000..2b25263 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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()); + } + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index e3a95d4..09e3dcd 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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))); diff --git a/src/main/resources/i18n/sone.en.properties b/src/main/resources/i18n/sone.en.properties index b1c4243..4db1a07 100644 --- a/src/main/resources/i18n/sone.en.properties +++ b/src/main/resources/i18n/sone.en.properties @@ -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… diff --git a/src/main/resources/templates/imageBrowser.html b/src/main/resources/templates/imageBrowser.html index c2257bd..53126a8 100644 --- a/src/main/resources/templates/imageBrowser.html +++ b/src/main/resources/templates/imageBrowser.html @@ -65,6 +65,7 @@ <%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 index 0000000..45eb5f2 --- /dev/null +++ b/src/main/resources/templates/include/uploadImage.html @@ -0,0 +1,12 @@ +

<%= View.UploadImage.Title|l10n|html>

+ +
+ + + + + + + + +
-- 2.7.4