Skip to content

Commit 4969258

Browse files
committed
Bugfix: Add mime type preamble to base64 strings
1 parent 09baa9d commit 4969258

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

Tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Base64
1313
O> bitmap:copy-to-pcolors image true
1414
O> set pcolors map [ p -> [pcolor] of p ] (sort patches)
1515
O> set base64 bitmap:to-base64 image
16-
substring base64 0 49 => "iVBORw0KGgoAAAANSUhEUgAAAEAAAAAwCAYAAAChS3wfAAAnc"
16+
substring base64 0 49 => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEA"
1717
O> set image bitmap:from-base64 base64
1818
O> bitmap:copy-to-pcolors image true
1919
pcolors = map [ p -> [pcolor] of p ] (sort patches) => true

src/main/BitmapExtension.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ public String getAgentClassString() {
128128

129129
public Object report(Argument args[], Context context)
130130
throws ExtensionException, LogoException {
131-
String base64 = args[0].getString();
132-
byte[] bytes = Base64.getDecoder().decode(base64);
131+
String[] splits = args[0].getString().split(",");
132+
if (splits.length != 2) {
133+
throw new ExtensionException("Base 64 string must start with a preamble like 'data:image/png;base64,...'");
134+
}
135+
String base64 = splits[1];
136+
byte[] bytes = Base64.getDecoder().decode(base64);
133137
try {
134138
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
135139
return new LogoBitmap(image);
@@ -159,7 +163,8 @@ public Object report(Argument args[], Context context)
159163

160164
try {
161165
ImageIO.write(image, "png", baos);
162-
return Base64.getEncoder().encodeToString(baos.toByteArray());
166+
String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
167+
return "data:image/png;base64," + base64;
163168
}
164169
catch (final IOException ex) {
165170
throw new ExtensionException(ex);

0 commit comments

Comments
 (0)