Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. var promisify = require('promisify-node'),
  2. PromiseFtp = require('promise-ftp'),
  3. fs = promisify('fs')
  4.  
  5. var _ftp = new PromiseFtp(),
  6. _localFilePath = './data/',
  7. _remoteFilePath = '/public_html/temp/'
  8.  
  9. function Ftp(env){
  10. this.config = {
  11. host: 'host',
  12. user: 'user',
  13. password: 'password'
  14. }
  15. }
  16.  
  17. Ftp.prototype.mput = function(fileList){
  18. _ftp.connect(this.config)
  19. .then(() => multiPutFiles(fileList))
  20. .then(() => {
  21. // clear cache here
  22. console.log('disconnecting...')
  23. return _ftp.end()
  24. })
  25. // connection errors
  26. .catch((err) => { console.log(err.toString()) })
  27. }
  28.  
  29. Ftp.prototype.publish = function(ext){
  30. return fs.readdir(_localFilePath)
  31. .then(function(files){
  32. var pattern = new RegExp('.' + ext)
  33. return files.filter((file) => pattern.test(file))
  34. })
  35. }
  36.  
  37. function multiPutFiles(fileList){
  38. return new Promise(function(resolve, reject){
  39. var chain = Promise.resolve()
  40.  
  41. fileList.forEach(function(file, i, arr){
  42. chain = chain.then(() => {
  43. console.log('uploading:', _localFilePath + file)
  44. return _ftp.put(_localFilePath + file, _remoteFilePath + file)
  45. })
  46. // file upload errors
  47. .catch((err) => { console.log(err.toString()) })
  48.  
  49. if(i === arr.length - 1)
  50. chain.then(() => resolve())
  51. })
  52. })
  53. }
  54.  
  55. //var ftp = new Ftp('LIVE');
  56. //ftp.publish('json').done((files) => ftp.mput(files))
  57. //ftp.mput(['test.txt', 'test1.txt'])
  58.  
  59. module.exports = Ftp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement