File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -1985,7 +1985,23 @@ static protected PImage parseBase64Image(String imagePath) {
1985
1985
String extension = parts [0 ].substring (11 );
1986
1986
String encodedData = parts [1 ];
1987
1987
1988
- byte [] decodedBytes = Base64 .getDecoder ().decode (encodedData );
1988
+ // If a SVG image has an error, the base 64 will fail, but the browser will work.
1989
+ // So we are sanitizing the values before reading it.
1990
+ // If you have any questions, please, read this
1991
+ // reference: https://www.prostdev.com/post/understanding-the-illegal-base64-character-error-java-groovy-and-mule-4-dw-2-0
1992
+ Base64 .Decoder decoder ;
1993
+ if (encodedData .contains ("+" ) || encodedData .contains ("/" )) {
1994
+ decoder = Base64 .getDecoder ();
1995
+ encodedData = encodedData .replaceAll ("[^A-Za-z0-9+/=]" , "" );
1996
+ } else if (encodedData .contains ("-" ) || encodedData .contains ("_" )) {
1997
+ decoder = Base64 .getUrlDecoder ();
1998
+ encodedData = encodedData .replaceAll ("[^A-Za-z0-9-_=]" , "" );
1999
+ } else {
2000
+ decoder = Base64 .getDecoder ();
2001
+ encodedData = encodedData .replaceAll ("[^A-Za-z0-9+/=]" , "" );
2002
+ }
2003
+
2004
+ byte [] decodedBytes = decoder .decode (encodedData );
1989
2005
1990
2006
if (decodedBytes == null ) {
1991
2007
System .err .println ("Decode Error on image: " + imagePath .substring (0 , 20 ));
You can’t perform that action at this time.
0 commit comments