From be8a554a46daf43eb0cd5c9e7dd034e3455dd477 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 12 Oct 2013 11:09:31 +0200 Subject: [PATCH] Make all members of TemporaryImage final, store width and height, too. --- .../java/net/pterodactylus/sone/core/Core.java | 5 +- .../pterodactylus/sone/data/TemporaryImage.java | 63 +++++++--------------- .../pterodactylus/sone/web/UploadImagePage.java | 6 ++- 3 files changed, 26 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 7e93410..fac14a9 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1672,9 +1672,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The encoded data of the image * @return The temporary image */ - public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) { - TemporaryImage temporaryImage = new TemporaryImage(); - temporaryImage.setMimeType(mimeType).setImageData(imageData); + public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData, int width, int height) { + TemporaryImage temporaryImage = new TemporaryImage(mimeType, imageData, width, height); synchronized (temporaryImages) { temporaryImages.put(temporaryImage.getId(), temporaryImage); } diff --git a/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java b/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java index 5c657ac..d5c540c 100644 --- a/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java +++ b/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java @@ -18,7 +18,6 @@ package net.pterodactylus.sone.data; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.util.UUID; @@ -34,26 +33,26 @@ public class TemporaryImage { private final String id; /** The MIME type of the image. */ - private String mimeType; + private final String mimeType; /** The encoded image data. */ - private byte[] imageData; + private final byte[] imageData; - /** - * Creates a new temporary image with a random ID. - */ - public TemporaryImage() { - this(UUID.randomUUID().toString()); - } + private final int width; + + private final int height; /** - * Creates a new temporary image. - * - * @param id - * The ID of the temporary image + * Creates a new temporary image with a random ID. + * @param imageData + * @param mimeType */ - public TemporaryImage(String id) { - this.id = id; + public TemporaryImage(String mimeType, byte[] imageData, int width, int height) { + this.id = UUID.randomUUID().toString(); + this.mimeType = checkNotNull(mimeType, "mime type must not be null"); + this.imageData = checkNotNull(imageData, "image data must not be null"); + this.width = width; + this.height = height; } /** @@ -75,21 +74,6 @@ public class TemporaryImage { } /** - * Sets the MIME type of the image. The MIME type can only be set once and - * it must not be {@code null}. - * - * @param mimeType - * The MIME type of the image - * @return This temporary image - */ - public TemporaryImage setMimeType(String mimeType) { - checkNotNull(mimeType, "mimeType must not be null"); - checkState(this.mimeType == null, "mime type must not already be set"); - this.mimeType = mimeType; - return this; - } - - /** * Returns the encoded image data. * * @return The encoded image data @@ -98,19 +82,12 @@ public class TemporaryImage { return imageData; } - /** - * Sets the encoded image data. The encoded image data can only be set once - * and it must not be {@code null}. - * - * @param imageData - * The encoded image data - * @return This temporary image - */ - public TemporaryImage setImageData(byte[] imageData) { - checkNotNull(imageData, "imageData must not be null"); - checkState(this.imageData == null, "image data must not already be set"); - this.imageData = imageData; - return this; + public int getWidth() { + return width; + } + + public int getHeight() { + return height; } } diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 3844082..3031970 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -121,9 +121,11 @@ public class UploadImagePage extends SoneTemplatePage { return; } String mimeType = getMimeType(imageData); - TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData); + int width = uploadedImage.getWidth(null); + int height = uploadedImage.getHeight(null); + TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData, width, height); image = webInterface.getCore().createImage(currentSone, parent, temporaryImage); - image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update(); + image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not read uploaded image!", ioe1); return; -- 2.7.4