Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 29 additions & 35 deletions src/main/java/net/rptools/maptool/client/functions/TokenImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ final int getValue() {
public static final String SET_IMAGE = "setImage";
public static final String SET_PORTRAIT = "setTokenPortrait";
public static final String SET_HANDOUT = "setTokenHandout";
public static final String FILE_HEADER_WEBP = "RIFF";
public static final String FILE_HEADER_JPG = "ÿØÿà";
public static final String FILE_HEADER_PNG = "‰PNG";

private TokenImage() {
super(
Expand Down Expand Up @@ -189,46 +186,39 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, args, 2, 2);
String imageName = args.get(0).toString();
String imageString = args.get(1).toString();
Asset asset = null;
if (imageName.isEmpty() || imageString.isEmpty()) {
throw new ParserException(
I18N.getText("macro.function.general.paramCannotBeEmpty", functionName));
} else if (imageString.length() > 8) {
Asset asset;
URI uri;
byte[] imageBytes = Base64.decode(imageString);
try {
uri = new URI(imageString);
} catch (URISyntaxException e) {
uri = null;
}
if (uri != null && isValidAssetScheme(uri) && isValidAssetExtension(uri)) {
try {
URL url = uri.toURL();
BufferedImage imageRAW = ImageIO.read(url);
asset = Asset.createImageAsset(imageName, imageRAW);
} catch (MalformedURLException | IllegalArgumentException e) {
throw new ParserException(
I18N.getText("macro.function.input.illegalArgumentType", imageString));
} catch (IOException e1) {
throw new ParserException(I18N.getText("macro.function.html5.invalidURI", imageString));
}
} else {
byte[] imageBytes = Base64.decode(imageString);
String imageCheck;
asset = Asset.createAssetDetectType(imageName, imageBytes);
} catch (Exception e) {
URI uri;
try {
imageCheck = new String(imageBytes, 0, 4);
} catch (Exception e) {
throw new ParserException(I18N.getText("dragdrop.unsupportedType", functionName));
uri = new URI(imageString);
} catch (URISyntaxException e1) {
uri = null;
}
if (imageCheck.equals(FILE_HEADER_WEBP)
|| imageCheck.equals(FILE_HEADER_JPG)
|| imageCheck.equals(FILE_HEADER_PNG)) {
asset = Asset.createImageAsset(imageName, imageBytes);
} else {
throw new ParserException(I18N.getText("dragdrop.unsupportedType", functionName));
if (uri != null && isValidAssetScheme(uri) && isValidAssetExtension(uri)) {
try {
URL url = uri.toURL();
BufferedImage imageRAW = ImageIO.read(url);
asset = Asset.createImageAsset(imageName, imageRAW);
} catch (MalformedURLException | IllegalArgumentException e2) {
throw new ParserException(
I18N.getText("macro.function.input.illegalArgumentType", imageString));
} catch (IOException e3) {
throw new ParserException(
I18N.getText("macro.function.html5.invalidURI", imageString));
}
}
}
AssetManager.putAsset(asset);
return "asset://" + asset.getMD5Key().toString();
if (asset != null) {
AssetManager.putAsset(asset);
return "asset://" + asset.getMD5Key().toString();
}
} else {
throw new ParserException(
I18N.getText("macro.function.general.wrongParamType", functionName));
Expand Down Expand Up @@ -388,9 +378,13 @@ private static Token findImageToken(final String name, String functionName) {
* @param uri The URI to check.
* @return {@code true} if the scheme is valid.
*/
private boolean isValidAssetScheme(URI uri) {
public boolean isValidAssetScheme(URI uri) {
if (uri == null || uri.getScheme() == null) {
return false;
}
return uri.getScheme().equalsIgnoreCase("http")
|| uri.getScheme().equalsIgnoreCase("https")
|| uri.getScheme().equalsIgnoreCase("asset")
|| uri.getScheme().equalsIgnoreCase("lib");
}

Expand Down