You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.3 KiB

module.exports = (shepherd) => {
/**
* Promise based download file method
*/
shepherd.downloadFile = (configuration) => {
return new shepherd.Promise((resolve, reject) => {
// Save variable to know progress
let receivedBytes = 0;
let totalBytes = 0;
let req = shepherd.request({
method: 'GET',
uri: configuration.remoteFile,
agentOptions: {
keepAlive: true,
keepAliveMsecs: 15000,
},
});
let out = shepherd.fs.createWriteStream(configuration.localFile);
req.pipe(out);
req.on('response', (data) => {
// Change the total bytes value to get progress later.
totalBytes = parseInt(data.headers['content-length']);
});
// Get progress if callback exists
if (configuration.hasOwnProperty('onProgress')) {
req.on('data', (chunk) => {
// Update the received bytes
receivedBytes += chunk.length;
configuration.onProgress(receivedBytes, totalBytes);
});
} else {
req.on('data', (chunk) => {
// Update the received bytes
receivedBytes += chunk.length;
});
}
req.on('end', () => {
resolve();
});
});
}
return shepherd;
};