Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Multiple file upload with extension filtering and the file must contain a object that have a base64 data and size
- const fs = require('fs')
- //dot env file
- const DATA_URL = process.env.DATA_URL || "http://localhost:3030" //create the data url of file
- const multipleImageFileUploader = (file, requireExtension) => { //here file contain a object which have a base64 property and size of that file and require extension contain a array
- let config = {
- dataUrl: DATA_URL, //set the default data url
- saveDirectory: `${__dirname}/../public`, //set the save directory of the data
- fileName: ``, //set the file name
- }
- let exportData = [];
- let finalExportData
- let checkExtension = fileFilter(file, requireExtension)
- const {extensionValidation} = checkExtension
- if(!extensionValidation){ //if one of the data's extension is not validated then it will execute
- finalExportData = {
- extensionValidation: false,
- fileUrl: null,
- fileAddStatus: false
- }
- }else{ //if all of the data's extension is validated then it will execute
- file.map(rawData => {
- const myFile = rawData //store the file here
- const {base64, size} = myFile //get the data from my data
- const dataExtension = base64.split(';')[0].split('/')[1] //get the extension of my data
- const myBase64Data = base64.split(';base64,')[1] //get the base 64 data of my data
- const myFileName = `${config.fileName}${+new Date()}.${dataExtension}` //set the file new name
- const myDataUrl = `${config.dataUrl}/${myFileName}` //set the data new data url
- const saveDirectory = `${config.saveDirectory}/${myFileName}` //which folder does file have been saved
- // console.log(saveDirectory);
- // upload the file here
- fs.writeFile(saveDirectory, myBase64Data, {encoding: "base64"}, (err) => {
- if(err){
- console.log(err);
- return err
- }else{
- console.log("File uploaded successfully");
- exportData.push(myDataUrl) //store each data into a array
- }
- })
- })
- finalExportData = {
- extensionValidation: true,
- fileUrl: exportData,
- fileAddStatus: true
- } //these data will be return
- return finalExportData
- }
- return finalExportData
- }
- const fileFilter = (file, extension) => { //here file and extension will be array
- const myFile = file //it will be an array
- let tracking = [] //track all extension filter boolean status true or false and store it to it
- myFile.map(data => {
- const {base64, size} = data //get the data from my data
- const dataExtension = base64.split(';')[0].split('/')[1] //get the extension of my data
- const isValid = extension.find(val => val == dataExtension) //check that is it a valid extension or not
- if(isValid){
- tracking.push(true)
- }else{
- tracking.push(false)
- }
- })
- const isNotMatch = tracking.find(ele => ele == false) //find the false value from the tracking array
- if(isNotMatch == false){
- return {
- extensionValidation: false
- }
- }else{
- return{
- extensionValidation: true
- }
- }
- }
- module.exports = {multipleImageFileUploader}FileUploader}
Add Comment
Please, Sign In to add comment