/** * Input File to base64 * ============================================================================== * * @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. */ export default async function inputFileToBase64({ inputFile, allowedRegex, }) { var _a; const allowedTypesRegex = allowedRegex ? allowedRegex : /image\/*|\/pdf/; if (!((_a = inputFile === null || inputFile === void 0 ? void 0 : inputFile.type) === null || _a === void 0 ? void 0 : _a.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 { fileName: inputFile.name, }; } try { let fileName = inputFile.name.replace(/\..*/, ""); const fileData = await new Promise((resolve, reject) => { var reader = new FileReader(); reader.readAsDataURL(inputFile); reader.onload = function () { var _a; resolve((_a = reader.result) === null || _a === void 0 ? void 0 : _a.toString()); }; reader.onerror = function (/** @type {*} */ error) { console.log("Error: ", error.message); }; }); return { fileBase64: fileData === null || fileData === void 0 ? void 0 : fileData.replace(/.*?base64,/, ""), fileBase64Full: fileData, fileName: fileName, fileSize: inputFile.size, fileType: inputFile.type, }; } catch (error) { console.log("File Processing Error! =>", error.message); return { fileName: inputFile.name, }; } }