2021-09-21 09:22:35 -04:00
|
|
|
/*
|
|
|
|
From https://github.com/DiegoZoracKy/image-data-uri/blob/master/lib/image-data-uri.js
|
|
|
|
Modified with 0 dependencies
|
|
|
|
*/
|
|
|
|
let fs = require("fs");
|
2022-04-13 11:33:37 -04:00
|
|
|
const { log } = require("../src/util");
|
2021-09-21 09:22:35 -04:00
|
|
|
|
|
|
|
let ImageDataURI = (() => {
|
|
|
|
|
2021-11-10 00:24:31 -05:00
|
|
|
/**
|
|
|
|
* @param {string} dataURI - A string that is a valid Data URI.
|
|
|
|
* @returns {?Object} An object with properties "imageType" and "dataBase64". The former is the image type, e.g., "png", and the latter is a base64 encoded string of the image's binary data. If it fails to parse, returns null instead of an object.
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2021-09-21 09:22:35 -04:00
|
|
|
function decode(dataURI) {
|
|
|
|
if (!/data:image\//.test(dataURI)) {
|
2022-04-13 11:33:37 -04:00
|
|
|
log.error("image-data-uri", "It seems that it is not an Image Data URI. Couldn't match \"data:image/\"");
|
2021-09-21 09:22:35 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let regExMatches = dataURI.match("data:(image/.*);base64,(.*)");
|
|
|
|
return {
|
|
|
|
imageType: regExMatches[1],
|
|
|
|
dataBase64: regExMatches[2],
|
|
|
|
dataBuffer: new Buffer(regExMatches[2], "base64")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:24:31 -05:00
|
|
|
/**
|
|
|
|
* @param {Buffer} data - The image data to be encoded.
|
|
|
|
* @param {String} mediaType - The type of the image, e.g., "image/png".
|
|
|
|
* @returns {String|null} A string representing the base64-encoded version of the given Buffer object or null if an error occurred.
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2021-09-21 09:22:35 -04:00
|
|
|
function encode(data, mediaType) {
|
|
|
|
if (!data || !mediaType) {
|
2022-04-13 11:33:37 -04:00
|
|
|
log.error("image-data-uri", "Missing some of the required params: data, mediaType");
|
2021-09-21 09:22:35 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaType = (/\//.test(mediaType)) ? mediaType : "image/" + mediaType;
|
|
|
|
let dataBase64 = (Buffer.isBuffer(data)) ? data.toString("base64") : new Buffer(data).toString("base64");
|
|
|
|
let dataImgBase64 = "data:" + mediaType + ";base64," + dataBase64;
|
|
|
|
|
|
|
|
return dataImgBase64;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:24:31 -05:00
|
|
|
/**
|
|
|
|
* Converts a data URI to a file path.
|
|
|
|
* @param {string} dataURI The Data URI of the image.
|
|
|
|
* @param {string} [filePath] The path where the image will be saved, defaults to "./".
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2021-09-21 09:22:35 -04:00
|
|
|
function outputFile(dataURI, filePath) {
|
|
|
|
filePath = filePath || "./";
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let imageDecoded = decode(dataURI);
|
|
|
|
|
|
|
|
fs.writeFile(filePath, imageDecoded.dataBuffer, err => {
|
|
|
|
if (err) {
|
|
|
|
return reject("ImageDataURI :: Error :: " + JSON.stringify(err, null, 4));
|
|
|
|
}
|
|
|
|
resolve(filePath);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
decode: decode,
|
|
|
|
encode: encode,
|
|
|
|
outputFile: outputFile,
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = ImageDataURI;
|