From 5f9213b0b925f84e2a76915058baafc23264b043 Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Tue, 13 Nov 2012 07:06:34 +0100 Subject: [PATCH] Use image data when recreating databases for testing. * src/main/java/org/glom/web/shared/DataItem.java: Add get/setImageData(), for use locally (not acrosss the network) when recreating databases for testing. getValue(): Use it here instead of getImageDataUrl(), noting that this method is only for use when recreating databases. * src/main/java/org/glom/web/server/libglom/Document.java: getNodeTextChildAsValue(): Use it instead of setImageDataUrl(). --- ChangeLog | 12 +++++++++ .../java/org/glom/web/server/libglom/Document.java | 30 +++++++++++++++------- src/main/java/org/glom/web/shared/DataItem.java | 20 ++++++++++++++- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6963543..fc7797e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2012-11-12 Murray Cumming + + Use image data when recreating databases for testing. + + * src/main/java/org/glom/web/shared/DataItem.java: Add get/setImageData(), + for use locally (not acrosss the network) when recreating databases + for testing. + getValue(): Use it here instead of getImageDataUrl(), noting that + this method is only for use when recreating databases. + * src/main/java/org/glom/web/server/libglom/Document.java: + getNodeTextChildAsValue(): Use it instead of setImageDataUrl(). + 2012-11-09 Murray Cumming OnlineGlomImages: Detect the mime-type (content type). diff --git a/src/main/java/org/glom/web/server/libglom/Document.java b/src/main/java/org/glom/web/server/libglom/Document.java index 0348474..7fffcfe 100644 --- a/src/main/java/org/glom/web/server/libglom/Document.java +++ b/src/main/java/org/glom/web/server/libglom/Document.java @@ -19,8 +19,11 @@ package org.glom.web.server.libglom; +//import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +//import java.io.InputStream; +//import java.net.URLConnection; import java.text.DateFormat; import java.text.NumberFormat; import java.text.ParseException; @@ -442,6 +445,8 @@ public class Document { } } + // We do not normally use this, + // though we do use it during testing, in SelfHoster, to recreate the database data. final Element exampleRowsNode = getElementByName(tableNode, NODE_EXAMPLE_ROWS); if (exampleRowsNode != null) { @@ -520,16 +525,23 @@ public class Document { break; } case TYPE_IMAGE: { - /* TODO: - final byte[] imageByteArray = null; // TODO. - if (imageByteArray != null) { - String base64 = com.google.gwt.user.server.Base64Utils.toBase64(imageByteArray); - base64 = "data:image/png;base64," + base64; - result.setImageDataUrl(base64); - } else { - result.setImageDataUrl(null); - } + //Glom (at least since 2.23/24) uses base64 for the images: + + //Discover the mime type: + final byte[] bytes = com.google.gwt.user.server.Base64Utils.fromBase64(unescaped); + /* + final InputStream is = new ByteArrayInputStream(bytes); + String contentType = ""; + try { + contentType = URLConnection.guessContentTypeFromStream(is); + } catch (IOException e) { + Log.error("getNodeTextChildAsValue(): unrecognised image data content type."); + } + + final String base64 = "data:" + contentType + ";base64," + unescaped; + result.setImageDataUrl(base64); */ + result.setImageData(bytes); break; } case TYPE_NUMERIC: { diff --git a/src/main/java/org/glom/web/shared/DataItem.java b/src/main/java/org/glom/web/shared/DataItem.java index ed52fc9..9acb230 100644 --- a/src/main/java/org/glom/web/shared/DataItem.java +++ b/src/main/java/org/glom/web/shared/DataItem.java @@ -35,6 +35,7 @@ public class DataItem implements Serializable { private boolean bool; private double number; private Date date; + private byte[] imageData; //This is only used locally to recreate database data. private String imageDataUrl; //This is for use as an or GWT Image URL. // TODO: Time @@ -73,6 +74,18 @@ public class DataItem implements Serializable { public void setDate(final Date date) { this.date = date; } + + /** This is not used in DataItem instances that are passed from the server to the client. + * This is only used locally to recreate database data. + * @param bytes + */ + public void setImageData(final byte[] imageData) { + this.imageData = imageData; + } + + public byte[] getImageData() { + return imageData; + } public String getImageDataUrl() { return imageDataUrl; @@ -83,12 +96,17 @@ public class DataItem implements Serializable { this.imageDataUrl = image; } + /** This is used by SelfHoster to get data for a database column. + * + * @param type The expected type of the data. + * @return The data. + */ public Object getValue(final Field.GlomFieldType type) { switch (type) { case TYPE_BOOLEAN: return getBoolean(); case TYPE_IMAGE: - return getImageDataUrl(); + return getImageData(); //getImageDataUrl() is for use on the client side only. case TYPE_NUMERIC: return getNumber(); case TYPE_TEXT: -- 2.1.4