Created
September 3, 2018 12:17
-
-
Save david1542/656b5a2a5e0d4313dfc72c2147d88ce4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetchFiles(path) { | |
return new Promise(function(resolve, reject) { | |
// Fetch started. Listing all files from the user specific directory. | |
ftp.ls(path, function(err, res) { | |
if(err || res.length === 0) return reject(); | |
// Close the ftp connection | |
ftp.destroy(); | |
// Iterating files list | |
async.mapSeries(res, function(file, callback) { | |
let buffer = false; | |
const connection = new JSFtp(ftpInfo); | |
connection.get(path + '/' + file.name, function(err, socket) { | |
if(err) return reject(err); | |
socket.on('data', function(data) { | |
if (!buffer) buffer = data; | |
else buffer = Buffer.concat([buffer, data]); | |
}); | |
socket.on('close', function(err) { | |
if(err) { | |
return callback(err); | |
} | |
callback(null, buffer); | |
connection.destroy(); | |
}); | |
socket.resume(); | |
}); | |
}, function(err, buffers) { | |
if(err) { | |
return reject(err); | |
} else { | |
const result = res.map(function(file, index) { | |
file.buffer = buffers[index]; | |
return file; | |
}); | |
resolve(result); | |
} | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment