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