0
   

Control seems to stop after last console.log. How would I return the program flow back to ftp.auth f

 
 
kanalas
 
Reply Fri 19 May, 2017 09:35 am
ftp.auth(username, password, function(err) {

//zip files locally
output = fs.createWriteStream('./bravo.zip');
const archive = archiver('zip', {
store: true // Sets the compression method to STORE.
});

archive.on('error', function(err) {
if (err) {
console.log(err);
return;
}
});

archive.pipe(output);

if (err) {
console.log("Error", err);
return;
}

// list files from server path, then loop thru files and use ftp.get to
// fetch them.

// inside ftp.get callback you will use fs to write file to directory to
// test the output.

// loop thru and append them to archive, (testing) -> fs, (live) -> ftp,
// buffer temp files.

var gatherFiles = function(dir) {
return new Promise(function(resolve, reject) {
ftp.ls(dir, function(err, res) {
if (err) {
reject(err);
return;
}
// console.log(res);
var files = [];
res.forEach(function(file) {
files.push(file.name);
});
resolve(files);
});
});
}

// map to each file and stream the data through a socket as a buffer
// to store them directly in the zip archive.
gatherFiles(path).then(function(files) {
async.mapLimit(files, 1, function(file, callback) {

var bufs = [];
ftp.get(path + file, function(err, socket) {
if (err) {
console.log(err);
return;
}
socket.on("data", function(d) {
bufs.push(d);
});
socket.on("close", function(hadErr) {
if (hadErr) {
console.error('There was an error retrieving the file.');
callback(hadErr);
} else {
var buf = Buffer.concat(bufs);
archive.append(buf, {
name: file
});
callback();
}
});
socket.resume();
});
}, function(err, res) {
if (err) {
console.log(err);
return;
}
archive.finalize();
console.log('updates complete' + res);
});
});
});
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 972 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » Control seems to stop after last console.log. How would I return the program flow back to ftp.auth f
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/19/2024 at 02:39:58