X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FUploadImagePage.java;h=673e439ea2b3c6381affd7d83bc42928cac7af65;hb=00a434a23c9ea1e57c63d8a3c0fc4b09277af431;hp=2079cacadb3cc79880c37f2202ad4c8421f0379a;hpb=00a7e47e114cf24f9ab5731bcbe1abeafe435577;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 2079cac..673e439 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.web; +import static com.google.common.base.Optional.fromNullable; +import static java.util.logging.Logger.getLogger; + import java.awt.Image; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -31,16 +34,18 @@ import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.IdBuilder; +import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.page.FreenetRequest; 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 net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; import com.google.common.io.ByteStreams; import freenet.support.api.Bucket; @@ -53,8 +58,8 @@ import freenet.support.api.HTTPUploadedFile; */ public class UploadImagePage extends SoneTemplatePage { - /** The logger. */ - private static final Logger logger = Logging.getLogger(UploadImagePage.class); + private static final Logger logger = getLogger(UploadImagePage.class.getName()); + private static final String UNKNOWN_MIME_TYPE = "application/octet-stream"; /** * Creates a new “upload image” page. @@ -80,15 +85,13 @@ public class UploadImagePage extends SoneTemplatePage { 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) { - /* TODO - signal error */ - return; + String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH); + Optional parent = webInterface.getCore().getAlbum(parentId); + if (!parent.isPresent()) { + throw new RedirectException("noPermission.html"); } - if (!currentSone.equals(parent.getSone())) { - /* TODO - signal error. */ - return; + if (!currentSone.equals(parent.get().getSone())) { + throw new RedirectException("noPermission.html"); } String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200); String description = request.getHttpRequest().getPartAsStringFailsafe("description", 4000); @@ -121,16 +124,18 @@ public class UploadImagePage extends SoneTemplatePage { } String mimeType = getMimeType(imageData); TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData); - net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent, temporaryImage); + net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent.get(), temporaryImage); image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update(); } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not read uploaded image!", ioe1); return; + } catch (ImageTitleMustNotBeEmpty itmnbe) { + throw new RedirectException("emptyImageTitle.html"); } finally { Closer.close(imageDataInputStream); Closer.flush(uploadedImage); } - throw new RedirectException("imageBrowser.html?album=" + parent.getId()); + throw new RedirectException("imageBrowser.html?album=" + parent.get().getId()); } } @@ -152,12 +157,13 @@ public class UploadImagePage extends SoneTemplatePage { ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream); Iterator imageReaders = ImageIO.getImageReaders(imageInputStream); if (imageReaders.hasNext()) { - return imageReaders.next().getOriginatingProvider().getMIMETypes()[0]; + return fromNullable(imageReaders.next().getOriginatingProvider().getMIMETypes()) + .or(new String[] { UNKNOWN_MIME_TYPE })[0]; } } catch (IOException ioe1) { logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1); } - return "application/octet-stream"; + return UNKNOWN_MIME_TYPE; } }