@@ -65,9 +65,6 @@ final int getValue() {
6565 public static final String SET_IMAGE = "setImage" ;
6666 public static final String SET_PORTRAIT = "setTokenPortrait" ;
6767 public static final String SET_HANDOUT = "setTokenHandout" ;
68- public static final String FILE_HEADER_WEBP = "RIFF" ;
69- public static final String FILE_HEADER_JPG = "ÿØÿà" ;
70- public static final String FILE_HEADER_PNG = "‰PNG" ;
7168
7269 private TokenImage () {
7370 super (
@@ -189,46 +186,39 @@ public Object childEvaluate(
189186 FunctionUtil .checkNumberParam (functionName , args , 2 , 2 );
190187 String imageName = args .get (0 ).toString ();
191188 String imageString = args .get (1 ).toString ();
189+ Asset asset = null ;
192190 if (imageName .isEmpty () || imageString .isEmpty ()) {
193191 throw new ParserException (
194192 I18N .getText ("macro.function.general.paramCannotBeEmpty" , functionName ));
195193 } else if (imageString .length () > 8 ) {
196- Asset asset ;
197- URI uri ;
194+ byte [] imageBytes = Base64 .decode (imageString );
198195 try {
199- uri = new URI (imageString );
200- } catch (URISyntaxException e ) {
201- uri = null ;
202- }
203- if (uri != null && isValidAssetScheme (uri ) && isValidAssetExtension (uri )) {
204- try {
205- URL url = uri .toURL ();
206- BufferedImage imageRAW = ImageIO .read (url );
207- asset = Asset .createImageAsset (imageName , imageRAW );
208- } catch (MalformedURLException | IllegalArgumentException e ) {
209- throw new ParserException (
210- I18N .getText ("macro.function.input.illegalArgumentType" , imageString ));
211- } catch (IOException e1 ) {
212- throw new ParserException (I18N .getText ("macro.function.html5.invalidURI" , imageString ));
213- }
214- } else {
215- byte [] imageBytes = Base64 .decode (imageString );
216- String imageCheck ;
196+ asset = Asset .createAssetDetectType (imageName , imageBytes );
197+ } catch (Exception e ) {
198+ URI uri ;
217199 try {
218- imageCheck = new String ( imageBytes , 0 , 4 );
219- } catch (Exception e ) {
220- throw new ParserException ( I18N . getText ( "dragdrop.unsupportedType" , functionName )) ;
200+ uri = new URI ( imageString );
201+ } catch (URISyntaxException e1 ) {
202+ uri = null ;
221203 }
222- if (imageCheck .equals (FILE_HEADER_WEBP )
223- || imageCheck .equals (FILE_HEADER_JPG )
224- || imageCheck .equals (FILE_HEADER_PNG )) {
225- asset = Asset .createImageAsset (imageName , imageBytes );
226- } else {
227- throw new ParserException (I18N .getText ("dragdrop.unsupportedType" , functionName ));
204+ if (uri != null && isValidAssetScheme (uri ) && isValidAssetExtension (uri )) {
205+ try {
206+ URL url = uri .toURL ();
207+ BufferedImage imageRAW = ImageIO .read (url );
208+ asset = Asset .createImageAsset (imageName , imageRAW );
209+ } catch (MalformedURLException | IllegalArgumentException e2 ) {
210+ throw new ParserException (
211+ I18N .getText ("macro.function.input.illegalArgumentType" , imageString ));
212+ } catch (IOException e3 ) {
213+ throw new ParserException (
214+ I18N .getText ("macro.function.html5.invalidURI" , imageString ));
215+ }
228216 }
229217 }
230- AssetManager .putAsset (asset );
231- return "asset://" + asset .getMD5Key ().toString ();
218+ if (asset != null ) {
219+ AssetManager .putAsset (asset );
220+ return "asset://" + asset .getMD5Key ().toString ();
221+ }
232222 } else {
233223 throw new ParserException (
234224 I18N .getText ("macro.function.general.wrongParamType" , functionName ));
@@ -388,9 +378,13 @@ private static Token findImageToken(final String name, String functionName) {
388378 * @param uri The URI to check.
389379 * @return {@code true} if the scheme is valid.
390380 */
391- private boolean isValidAssetScheme (URI uri ) {
381+ public boolean isValidAssetScheme (URI uri ) {
382+ if (uri == null || uri .getScheme () == null ) {
383+ return false ;
384+ }
392385 return uri .getScheme ().equalsIgnoreCase ("http" )
393386 || uri .getScheme ().equalsIgnoreCase ("https" )
387+ || uri .getScheme ().equalsIgnoreCase ("asset" )
394388 || uri .getScheme ().equalsIgnoreCase ("lib" );
395389 }
396390
0 commit comments