const formidable = require('formidable');
const path = require('path');
const fs = require('fs');

function uploadMiddleware(req,res,next){
    const form = formidable({ multiples: true, keepExtensions: true, uploadDir: path.resolve(process.cwd(), 'uploads')});
    form.parse(req, (err, fields, files) => {
        if (err) {
          res.status(400);
          return res.send({Error : "Cannot Process your request"});
        }
        req.body = fields;
        req.file = files;
        next();
      });
}


function cleanUpUpload(path){
  fs.unlink(path, (err => {
    if (err) console.log(err);
    else {
      console.log("\nFILE DELETED FROM LOCAL SERVER.");
    }
  }));
}


function uploadToS3(){
  
}

module.exports = {
    uploadMiddleware :  uploadMiddleware,
    cleanUpUpload:  cleanUpUpload
}