To check whether the given value is url or not:
if (imageFieldId) {
let urlPattern = /^(https?|ftp|file):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/;
// This line creates a regular expression 'urlPattern' that is used to validate URLs.
// The regular expression checks for URLs starting with 'http://', 'https://', 'ftp://' or 'file://'.
// It then matches the domain name, and optionally matches paths and query parameters.
let imageContents;
let curImageType;
if (urlPattern.test(imageFieldId)) {
// If the value is a URL, convert it to base64
let imageData = convertUrlToBase64(imageFieldId);
imageContents = imageData.base64Data;
log.debug("imageContents url",imageContents);
curImageType = imageData.imageType;
log.debug("curImageType url",curImageType);
}
The code checks whether the variable imageFieldId contains a valid URL.
If it does, it converts the URL to base64 format using the convertUrlToBase64 function and then logs the resulting base64 data and image type.