firmament-vita
Version:
Firmament module for automating VITA tasks
133 lines • 6.37 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
var inversify_1 = require("inversify");
var child_process_1 = require("child_process");
var firmament_yargs_1 = require("firmament-yargs");
var fs = require('fs');
var path = require('path');
var async = require('async');
var mkdirp = require('mkdirp');
var through2Filter = require('through2-filter');
var _ = require('lodash');
var VitaDecryptUnTarImpl = (function (_super) {
__extends(VitaDecryptUnTarImpl, _super);
function VitaDecryptUnTarImpl(vitaSpawn, commandUtil, vitaFileUtil) {
var _this = _super.call(this) || this;
_this.vitaSpawn = vitaSpawn;
_this.commandUtil = commandUtil;
_this.vitaFileUtil = vitaFileUtil;
return _this;
}
VitaDecryptUnTarImpl.prototype.process = function (options, cbStatus, cbFinal) {
var _this = this;
cbFinal = this.checkCallback(cbFinal);
cbStatus = this.checkCallback(cbStatus);
var fnArray = [];
options.targetFolder = options.targetFolder || null;
options.encryptedFiles = options.encryptedFiles || [];
options.encryptedFilePattern = options.encryptedFilePattern || /\.enc$/;
options.foldersOfEncryptedFiles = options.foldersOfEncryptedFiles || [];
options.encryptedFiles = this.vitaFileUtil.findFilesSync({
pattern: options.encryptedFilePattern,
files: options.encryptedFiles,
folders: options.foldersOfEncryptedFiles
});
options.encryptedFiles.forEach(function (file) {
fnArray.push(async.apply(_this.spawnDecryptAndUnTarOperation.bind(_this), file, options.password, options.targetFolder, function (err, decryptAndUnTarStatus) {
decryptAndUnTarStatus.decryptAndUnTarOptions = _.omit(options, ['password', 'encryptedFilePattern']);
cbStatus(err, decryptAndUnTarStatus);
}));
});
async.parallelLimit(fnArray, 3, cbFinal);
};
VitaDecryptUnTarImpl.prototype.spawnDecryptAndUnTarOperation = function (inFile, password, targetFolder, cbStatus, cbFinal) {
var _this = this;
cbStatus = this.checkCallback(cbStatus);
cbFinal = this.checkCallback(cbFinal);
if (!targetFolder) {
targetFolder = path.dirname(inFile);
}
mkdirp(targetFolder, function (err) {
if (_this.commandUtil.callbackIfError(cbFinal, err)) {
return;
}
var inputFileSize = fs.statSync(inFile)['size'];
var totalBytesStreamed = 0;
var openSslProcessExited = false;
var tarProcessExited = false;
var tarArgs = [
'x',
'--strip-components=3',
'-C',
targetFolder
];
var tarProcess = child_process_1.spawn('tar', tarArgs);
var openSslArgs = [
'aes-256-cbc',
'-d',
'-in',
inFile,
'-k',
password
];
var openSslProcess = child_process_1.spawn('openssl', openSslArgs);
tarProcess.on('close', function (code, signal) {
tarProcessExited = true;
if (openSslProcessExited) {
_this.callbackFromDecryptAndUnTarFiles(cbFinal, targetFolder);
}
});
openSslProcess.on('close', function (code, signal) {
openSslProcessExited = true;
if (tarProcessExited) {
_this.callbackFromDecryptAndUnTarFiles(cbFinal, targetFolder);
}
});
openSslProcess.stdout.pipe(through2Filter(function (dataChunk) {
totalBytesStreamed += dataChunk.length;
var taskName = "Decrypting [" + path.basename(inFile) + "] ";
var current = totalBytesStreamed;
var total = inputFileSize;
cbStatus(null, { taskName: taskName, current: current, total: total });
return true;
})).pipe(tarProcess.stdin);
});
};
VitaDecryptUnTarImpl.prototype.callbackFromDecryptAndUnTarFiles = function (cb, folder) {
this.vitaFileUtil.findFiles({ pattern: /\.gz$/, folders: [folder] }, function (err, files) {
cb(err, { exportDir: folder, zipFiles: files });
});
};
VitaDecryptUnTarImpl = __decorate([
inversify_1.injectable(),
__param(0, inversify_1.inject('VitaSpawn')),
__param(1, inversify_1.inject('CommandUtil')),
__param(2, inversify_1.inject('VitaFileUtil')),
__metadata("design:paramtypes", [Object, Object, Object])
], VitaDecryptUnTarImpl);
return VitaDecryptUnTarImpl;
}(firmament_yargs_1.ForceErrorImpl));
exports.VitaDecryptUnTarImpl = VitaDecryptUnTarImpl;
//# sourceMappingURL=vita-decrypt-untar-impl.js.map