diff --git a/client/index.js b/client/index.js index 4eb1422..562b19c 100644 --- a/client/index.js +++ b/client/index.js @@ -5,6 +5,7 @@ */ const imageInputFileToBase64 = require("./media/imageInputFileToBase64"); const imageInputToBase64 = require("./media/imageInputToBase64"); +const inputFileToBase64 = require("./media/inputFileToBase64"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -21,6 +22,7 @@ const imageInputToBase64 = require("./media/imageInputToBase64"); const media = { imageInputToBase64: imageInputToBase64, imageInputFileToBase64: imageInputFileToBase64, + inputFileToBase64: inputFileToBase64, }; /** diff --git a/client/media/inputFileToBase64.js b/client/media/inputFileToBase64.js new file mode 100644 index 0000000..33a0ad8 --- /dev/null +++ b/client/media/inputFileToBase64.js @@ -0,0 +1,91 @@ +/** + * @typedef {{ + * fileBase64: string, + * fileBase64Full: string, + * fileName: string, + * fileSize: number, + * fileType: string, + * }} FunctionReturn + */ + +/** + * ============================================================================== + * Main Function + * ============================================================================== + * @async + * + * @param {{ + * inputFile: { + * name: string, + * size: number, + * type: string, + * }, + * }} params - Single object passed + * + * @description This function takes in a *SINGLE* input file from a HTML file input element. + * HTML file input elements usually return an array of input objects, so be sure to select the target + * file from the array. + * + * @returns { Promise } - Return Object + */ +module.exports = async function inputFileToBase64({ inputFile }) { + /** + * == Initialize + * + * @description Initialize + */ + const allowedTypesRegex = /image\/*|\/pdf/; + + if (!inputFile?.type?.match(allowedTypesRegex)) { + window.alert(`We currently don't support ${inputFile.type} file types. Support is coming soon. For now we support only images and PDFs.`); + + return { + fileBase64: null, + fileBase64Full: null, + fileName: inputFile.name, + fileSize: null, + fileType: null, + }; + } + + try { + /** + * == Process File + */ + let fileName = inputFile.name.replace(/\..*/, ""); + + /** Add source to new file **/ + const fileData = await new Promise((resolve, reject) => { + var reader = new FileReader(); + reader.readAsDataURL(inputFile); + reader.onload = function () { + resolve(reader.result); + }; + reader.onerror = function (error) { + console.log("Error: ", error.message); + }; + }); + + return { + fileBase64: fileData.replace(/.*?base64,/, ""), + fileBase64Full: fileData, + fileName: fileName, + fileSize: inputFile.size, + fileType: inputFile.type, + }; + } catch (error) { + console.log("Image Processing Error! =>", error.message); + + return { + fileBase64: null, + fileBase64Full: null, + fileName: inputFile.name, + fileSize: null, + fileType: null, + }; + } +}; + +/** ********************************************** */ +/** ********************************************** */ +/** ********************************************** */ diff --git a/package.json b/package.json index d30cc6a..c906a88 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.1.34", + "version": "1.1.35", "description": "Cloud-based SQL data management tool", "main": "index.js", "scripts": {