This commit is contained in:
Tben
2023-06-24 08:21:39 +01:00
parent f44d60626c
commit 6ab14af43b
5 changed files with 388 additions and 21 deletions
+11
View File
@@ -5,6 +5,7 @@
*/
import imageInputFileToBase64 from "./imageInputFileToBase64";
import imageInputToBase64 from "./imageInputToBase64";
import loginWithGoogle from "../auth/loginWithGoogle";
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -23,6 +24,16 @@ const media = {
imageInputFileToBase64: imageInputFileToBase64,
};
/**
* ==============================================================================
* Media Functions Object
* ==============================================================================
*/
const auth = {
imageInputToBase64: imageInputToBase64,
imageInputFileToBase64: imageInputFileToBase64,
};
/**
* ==============================================================================
* Main Export
+25 -20
View File
@@ -9,32 +9,31 @@
*/
/**
* Input File to base64
* ==============================================================================
* 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.
*
* @async
*
* @param {object} params - Single object passed
* @param {object} params.inputFile - HTML input File
* @param {string} params.inputFile.name - Input File Name
* @param {number} params.inputFile.size - Input File Size in bytes
* @param {string} params.inputFile.type - Input File Type: "JPEG", "PNG", "PDF", etc. Whichever allowed regexp is provided
* @param {RegExp} [params.allowedRegex] - Regexp containing the allowed file types
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
module.exports = async function inputFileToBase64({ inputFile }) {
module.exports = async function inputFileToBase64({ inputFile, allowedRegex }) {
/**
* == Initialize
*
* @description Initialize
*/
const allowedTypesRegex = /image\/*|\/pdf/;
const allowedTypesRegex = allowedRegex ? allowedRegex : /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.`);
@@ -49,9 +48,7 @@ module.exports = async function inputFileToBase64({ inputFile }) {
}
try {
/**
* == Process File
*/
/** Process File **/
let fileName = inputFile.name.replace(/\..*/, "");
/** Add source to new file **/
@@ -66,6 +63,10 @@ module.exports = async function inputFileToBase64({ inputFile }) {
};
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return {
fileBase64: fileData.replace(/.*?base64,/, ""),
fileBase64Full: fileData,
@@ -73,8 +74,12 @@ module.exports = async function inputFileToBase64({ inputFile }) {
fileSize: inputFile.size,
fileType: inputFile.type,
};
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
console.log("Image Processing Error! =>", error.message);
console.log("File Processing Error! =>", error.message);
return {
fileBase64: null,
@@ -86,6 +91,6 @@ module.exports = async function inputFileToBase64({ inputFile }) {
}
};
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////