const ftp = require("basic-ftp") 

const fs = require("fs");
async function sendToFtp(localpath ,filename,config){
    var client = new ftp.Client(config.upload?.ftp?.timeout||0)
   // client.ftp.verbose = true
    var remoteFile = config.upload.ftp.remoteFolder +'/'+filename
    return new Promise(async(resolve, reject)=>{
        try {
            await client.access({
                host: config.upload.ftp.host,
                user: config.upload.ftp.user,
                password: config.upload.ftp.password,
                secure: config.upload.ftp.secure,
                port:config.upload.ftp.port || 21,
                
            })
            await client.uploadFrom(localpath, remoteFile)
           
            client.close()
            if(!config.upload?.ftp?.keepLocalFile){
                fs.unlink(localpath, function(err) {
                    if (err) {
                      throw err
                    } else {
                      console.log("Successfully deleted the file.")
                    }
                  })
             }
        resolve()

        } catch(err){
           
           return  reject(err)
        }


    })

}


module.exports={

    sendToFtp
}