Better code, more flexible ArchiveUtil

This commit is contained in:
Bryan Ashby 2016-10-02 21:39:29 -06:00
parent c715bb7731
commit 35e7610670
1 changed files with 65 additions and 37 deletions

View File

@ -10,6 +10,37 @@ const fs = require('fs');
const _ = require('lodash');
const pty = require('ptyw.js');
let archiveUtil;
class Archiver {
constructor(config) {
this.compress = config.compress;
this.decompress = config.decompress;
this.list = config.list;
this.extract = config.extract;
this.sig = new Buffer(config.sig, 'hex');
this.offset = config.offset || 0;
}
ok() {
return this.canCompress() && this.canDecompress();
}
can(what) {
if(!_.has(this, [ what, 'cmd' ]) || !_.has(this, [ what, 'args' ])) {
return false;
}
return _.isString(this[what].cmd) && Array.isArray(this[what].args) && this[what].args.length > 0;
}
canCompress() { return this.can('compress'); }
canDecompress() { return this.can('decompress'); }
canList() { return this.can('list'); }
canExtract() { return this.can('extract'); }
}
module.exports = class ArchiveUtil {
constructor() {
@ -17,31 +48,28 @@ module.exports = class ArchiveUtil {
this.longestSignature = 0;
}
// singleton access
static getInstance() {
if(!archiveUtil) {
archiveUtil = new ArchiveUtil();
archiveUtil.init();
return archiveUtil;
}
}
init() {
//
// Load configuration
//
if(_.has(Config, 'archivers')) {
Object.keys(Config.archivers).forEach(archKey => {
const arch = Config.archivers[archKey];
if(!_.isString(arch.sig) ||
!_.isString(arch.compressCmd) ||
!_.isString(arch.decompressCmd) ||
!_.isArray(arch.compressArgs) ||
!_.isArray(arch.decompressArgs))
{
// :TODO: log warning
return;
}
const archiver = {
compressCmd : arch.compressCmd,
compressArgs : arch.compressArgs,
decompressCmd : arch.decompressCmd,
decompressArgs : arch.decompressArgs,
sig : new Buffer(arch.sig, 'hex'),
offset : arch.offset || 0,
};
const archConfig = Config.archivers[archKey];
const archiver = new Archiver(archConfig);
if(!archiver.ok()) {
// :TODO: Log warning - bad archiver/config
}
this.archivers[archKey] = archiver;
@ -65,6 +93,10 @@ module.exports = class ArchiveUtil {
return this.getArchiver(archType) ? true : false;
}
detectTypeWithBuf(buf, cb) {
// :TODO: implement me!
}
detectType(path, cb) {
fs.open(path, 'r', (err, fd) => {
if(err) {
@ -123,15 +155,13 @@ module.exports = class ArchiveUtil {
return cb(new Error(`Unknown archive type: ${archType}`));
}
let args = _.clone(archiver.compressArgs); // don't muck with orig
for(let i = 0; i < args.length; ++i) {
args[i] = stringFormat(args[i], {
const fmtObj = {
archivePath : archivePath,
fileList : files.join(' '),
});
}
};
let comp = pty.spawn(archiver.compressCmd, args, this.getPtyOpts());
const args = archiver.compress.args.map( arg => stringFormat(arg, fmtObj) );
const comp = pty.spawn(archiver.compress.cmd, args, this.getPtyOpts());
return this.spawnHandler(comp, 'Compression', cb);
}
@ -143,15 +173,13 @@ module.exports = class ArchiveUtil {
return cb(new Error(`Unknown archive type: ${archType}`));
}
let args = _.clone(archiver.decompressArgs); // don't muck with orig
for(let i = 0; i < args.length; ++i) {
args[i] = stringFormat(args[i], {
const fmtObj = {
archivePath : archivePath,
extractPath : extractPath,
});
}
};
let comp = pty.spawn(archiver.decompressCmd, args, this.getPtyOpts());
const args = archiver.decompress.args.map( arg => stringFormat(arg, fmtObj) );
const comp = pty.spawn(archiver.decompress.cmd, args, this.getPtyOpts());
return this.spawnHandler(comp, 'Decompression', cb);
}