shopnilSS

Multiple file upload

Jun 26th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Multiple file upload with extension filtering and the file must contain a object that have a base64 data and size
  2.  
  3.  const fs = require('fs')
  4.  
  5.  //dot env file
  6.  const DATA_URL = process.env.DATA_URL || "http://localhost:3030" //create the data url of file
  7.  
  8. 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
  9.     let config = {
  10.         dataUrl: DATA_URL, //set the default data url
  11.         saveDirectory: `${__dirname}/../public`, //set the save directory of the data
  12.         fileName: ``, //set the file name
  13.     }
  14.     let exportData = [];
  15.     let finalExportData
  16.     let checkExtension = fileFilter(file, requireExtension)
  17.     const {extensionValidation} = checkExtension
  18.    
  19.     if(!extensionValidation){ //if one of the data's  extension is not validated then it will execute
  20.         finalExportData =  {
  21.             extensionValidation: false,
  22.             fileUrl: null,
  23.             fileAddStatus: false
  24.         }
  25.     }else{  //if all of the data's extension is validated then it will execute
  26.         file.map(rawData => {
  27.             const myFile = rawData //store the file here
  28.             const {base64, size} = myFile //get the data from my data
  29.             const dataExtension = base64.split(';')[0].split('/')[1]  //get the extension of my data
  30.             const myBase64Data = base64.split(';base64,')[1] //get the base 64 data of my data
  31.             const myFileName = `${config.fileName}${+new Date()}.${dataExtension}` //set the file new name
  32.             const myDataUrl = `${config.dataUrl}/${myFileName}` //set the data new data url
  33.             const saveDirectory = `${config.saveDirectory}/${myFileName}` //which folder does file have been saved
  34.             // console.log(saveDirectory);
  35.             // upload the file here
  36.             fs.writeFile(saveDirectory, myBase64Data, {encoding: "base64"}, (err) => {
  37.                 if(err){
  38.                     console.log(err);
  39.                     return err
  40.                 }else{
  41.                     console.log("File uploaded successfully");
  42.                     exportData.push(myDataUrl) //store each data into a array
  43.                 }
  44.             })
  45.         })
  46.         finalExportData = {
  47.             extensionValidation: true,
  48.             fileUrl: exportData,
  49.             fileAddStatus: true
  50.         }  //these data will be return
  51.         return finalExportData
  52.     }
  53.     return finalExportData
  54. }
  55.  
  56. const fileFilter = (file, extension) => { //here file and extension will be array
  57.     const myFile = file //it will be an array
  58.     let tracking = [] //track all extension filter boolean status true or false and store it to it
  59.    
  60.     myFile.map(data => {
  61.         const {base64, size} = data //get the data from my data
  62.         const dataExtension = base64.split(';')[0].split('/')[1]  //get the extension of my data
  63.         const isValid = extension.find(val => val == dataExtension) //check that is it a valid extension or not
  64.         if(isValid){
  65.             tracking.push(true)
  66.         }else{
  67.             tracking.push(false)
  68.         }
  69.     })
  70.     const isNotMatch = tracking.find(ele => ele == false) //find the false value from the tracking array
  71.     if(isNotMatch == false){
  72.         return {
  73.             extensionValidation: false
  74.         }
  75.     }else{
  76.         return{
  77.             extensionValidation: true
  78.         }
  79.     }
  80. }
  81.  
  82. module.exports = {multipleImageFileUploader}FileUploader}
Add Comment
Please, Sign In to add comment