Add progress-stream for upload/download progress

This commit is contained in:
Girish Ramakrishnan
2017-04-21 10:50:25 -07:00
parent e15c3f05c2
commit 0aea7cc347
5 changed files with 149 additions and 144 deletions

View File

@@ -25,6 +25,7 @@ var assert = require('assert'),
mkdirp = require('mkdirp'),
once = require('once'),
path = require('path'),
progress = require('progress-stream'),
tar = require('tar-fs'),
zlib = require('zlib');
@@ -96,6 +97,7 @@ function backup(apiConfig, backupId, sourceDirectories, callback) {
var gzip = zlib.createGzip({});
var encrypt = crypto.createCipher('aes-256-cbc', apiConfig.key || '');
var progressStream = progress({ time: 10000 }); // display a progress every 10 seconds
pack.on('error', function (error) {
console.error('[%s] backup: tar stream error.', backupId, error);
@@ -112,12 +114,16 @@ function backup(apiConfig, backupId, sourceDirectories, callback) {
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
});
pack.pipe(gzip).pipe(encrypt);
progressStream.on('progress', function(progress) {
debug('[%s] backup: %s @ %s', backupId, Math.round(progress.transferred/1024/1024) + 'M', Math.round(progress.speed/1024/1024) + 'Mbps');
});
pack.pipe(gzip).pipe(encrypt).pipe(progressStream);
var params = {
Bucket: apiConfig.bucket,
Key: backupFilePath,
Body: encrypt
Body: progressStream
};
var s3 = new AWS.S3(credentials);
@@ -158,6 +164,7 @@ function restore(apiConfig, backupId, destination, callback) {
var s3get = s3.getObject(params).createReadStream();
var decrypt = crypto.createDecipher('aes-256-cbc', apiConfig.key || '');
var gunzip = zlib.createGunzip({});
var progressStream = progress({ time: 10000 }); // display a progress every 10 seconds
var extract = tar.extract(destination);
s3get.on('error', function (error) {
@@ -168,6 +175,10 @@ function restore(apiConfig, backupId, destination, callback) {
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
});
progressStream.on('progress', function(progress) {
debug('[%s] restore: %s @ %s', backupId, Math.round(progress.transferred/1024/1024) + 'M', Math.round(progress.speed/1024/1024) + 'Mbps');
});
decrypt.on('error', function (error) {
console.error('[%s] restore: decipher stream error.', error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
@@ -188,7 +199,7 @@ function restore(apiConfig, backupId, destination, callback) {
callback(null);
});
s3get.pipe(decrypt).pipe(gunzip).pipe(extract);
s3get.pipe(progressStream).pipe(decrypt).pipe(gunzip).pipe(extract);
});
});
}