SHARE
TWEET

Untitled

a guest Jun 5th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const JSFtp = require("jsftp");
  2. const path = require('path');
  3.  
  4. var ftp = new JSFtp({
  5.   host: process.env.HOST,
  6.   port: process.env.PORT,
  7.   user: process.env.USERNAME,
  8.   pass: process.env.PASSWORD
  9. });
  10.  
  11. ftp.auth(
  12.   ftp.user,
  13.   ftp.pass,
  14.   function (err, data) {
  15.     if (err) {
  16.       console.log("Error is: ", err);
  17.       return;
  18.     }
  19.  
  20.     removeAllFilesInPublicFolder(ftp);
  21.   }
  22. );
  23.  
  24. function removeAllFilesInPublicFolder(ftp) {
  25.   walk(ftp, '/public')
  26.     .then((results) => {
  27.       let files = flatten(results)
  28.         .filter(Boolean);
  29.  
  30.       console.log("About to try deleting ", files.map((file) => file.filepath));
  31.  
  32.       // Non-empty directories can't be removed, should I remove all directories first?
  33.       let deletions = files.map((file) => {
  34.         if (isDirectory(file)) {
  35.           return deleteDirectory(ftp, file);
  36.         } else {
  37.           return deleteFile(ftp, file);
  38.         }
  39.       });
  40.  
  41.       return Promise.all(deletions);
  42.     }).then((deletionResults) => {
  43.       console.log("Deletion results: ", deletionResults);
  44.       ftp.destroy();
  45.     }).catch((error) => {
  46.       console.log("Error: ", error);
  47.       ftp.destroy();
  48.     })
  49. }
  50.  
  51. function walk(ftp, directory) {
  52.   return list(ftp, directory)
  53.     .then((files) => {
  54.       if (files.length === 0) {
  55.         return Promise.resolve();
  56.       }
  57.  
  58.       return Promise.all(files.map((file) => {
  59.         file.filepath = path.join(directory, file.name);
  60.  
  61.         let promises = [];
  62.  
  63.         if (isDirectory(file)) {
  64.           promises.push(walk(ftp, path.join(directory, file.name)));
  65.         }
  66.         // Make sure the directory is after the files in the list of promises
  67.         promises.push(Promise.resolve(file));
  68.  
  69.         return Promise.all(promises);
  70.       }));
  71.     });
  72. }
  73.  
  74. function deleteDirectory(ftp, directory) {
  75.   return new Promise((resolve, reject) => {
  76.     ftp.raw('rmd', directory.filepath, (error, result) => {
  77.       if (error) {
  78.         return reject(error);
  79.       } else {
  80.         return resolve(result);
  81.       }
  82.     });
  83.   })
  84. }
  85.  
  86. function deleteFile(ftp, file) {
  87.   return new Promise((resolve, reject) => {
  88.     ftp.raw('dele', file.filepath, (error, result) => {
  89.       if (error) {
  90.         return reject(error);
  91.       } else {
  92.         return resolve(result);
  93.       }
  94.     });
  95.   })
  96. }
  97.  
  98. function list(ftp, directory) {
  99.   return new Promise((resolve, reject) => {
  100.     ftp.ls(directory, (error, files) => {
  101.       if (error) {
  102.         reject(error);
  103.         return;
  104.       }
  105.  
  106.       resolve(files);
  107.     });
  108.   });
  109. }
  110.  
  111. function isDirectory(file) {
  112.   return file.type === 1;
  113. }
  114.  
  115. const flatten = list => list.reduce(
  116.   (a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
  117. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top