Added 'zip' and 'unzip' console command

Bryan Roe committed Jun 24, 2020 at 16:04 UTC b18e302fcc4d5b05f3bab1198a1766d99c0cf1d4
1 file changed +43
agents/meshcore.js
+43
@@ -2238,6 +2238,7 @@ function createMeshCore(agent) {
2238 if (process.platform == 'win32') { availcommands += ',safemode,wpfhwacceleration,uac'; }
2239 if (process.platform != 'freebsd') { availcommands += ',vm';}
2240 if (require('MeshAgent').maxKvmTileSize != null) { availcommands += ',kvmmode'; }
2241 + try { require('zip-reader'); availcommands += ',zip,unzip'; } catch (xx) { }
2242
2243 availcommands = availcommands.split(',').sort();
2244 while (availcommands.length > 0) {
@@ -2248,6 +2249,48 @@ function createMeshCore(agent) {
2249 response = "Available commands: \r\n" + fin + ".";
2250 break;
2251 }
2252 + case 'zip':
2253 + if (args['_'].length == 0)
2254 + {
2255 + response = "Proper usage: zip (output file name), input1 [, input n]"; // Display usage
2256 + }
2257 + else
2258 + {
2259 + var p = args['_'].join(' ').split(',');
2260 + var ofile = p.shift();
2261 + sendConsoleText('Writing ' + ofile + '...');
2262 + var out = require('fs').createWriteStream(ofile, { flags: 'wb' });
2263 + out.fname = ofile;
2264 + out.sessionid = sessionid;
2265 + out.on('close', function () { sendConsoleText('DONE writing ' + this.fname, this.sessionid); });
2266 + var zip = require('zip-writer').write({ files: p });
2267 + zip.pipe(out);
2268 + }
2269 + break;
2270 + case 'unzip':
2271 + if (args['_'].length == 0)
2272 + {
2273 + response = "Proper usage: unzip input, destination"; // Display usage
2274 + }
2275 + else
2276 + {
2277 + var p = args['_'].join(' ').split(',');
2278 + if (p.length != 2)
2279 + {
2280 + response = "Proper usage: unzip input, destination"; // Display usage
2281 + break;
2282 + }
2283 + var prom = require('zip-reader').read(p[0]);
2284 + prom._dest = p[1];
2285 + prom.self = this;
2286 + prom.sessionid = sessionid;
2287 + prom.then(function (zipped)
2288 + {
2289 + sendConsoleText('Extracting to ' + this._dest + '...', this.sessionid);
2290 + zipped.extractAll(this._dest).then(function () { sendConsoleText('finished unzipping', this.sessionid); }, function (e) { sendConsoleText('Error unzipping: ' + e, this.sessionid); }).parentPromise.sessionid = this.sessionid;
2291 + }, function (e) { sendConsoleText('Error unzipping: ' + e, this.sessionid); });
2292 + }
2293 + break;
2294 case 'setbattery':
2295 // require('MeshAgent').SendCommand({ action: 'battery', state: 'dc', level: 55 });
2296 if ((args['_'].length > 0) && ((args['_'][0] == 'ac') || (args['_'][0] == 'dc'))) {