Fixed device file downloads on Linux

Ylian Saint-Hilaire committed Aug 28, 2020 at 18:29 UTC 5a0cdb8c64cf924812667058f4c8d8c2b7c43041
1 file changed +12 -2
agents/meshcore.js
+12 -2
@@ -1321,6 +1321,7 @@ function createMeshCore(agent) {
1321 // Basic file transfer
1322 //
1323 var stats = null;
1324 + if ((process.platform != 'win32') && (this.httprequest.xoptions.file.startsWith('/') == false)) { this.httprequest.xoptions.file = '/' + this.httprequest.xoptions.file; }
1325 try { stats = require('fs').statSync(this.httprequest.xoptions.file) } catch (e) { }
1326 try { if (stats) { this.httprequest.downloadFile = fs.createReadStream(this.httprequest.xoptions.file, { flags: 'rbN' }); } } catch (e) { }
1327 if (this.httprequest.downloadFile) {
@@ -2166,9 +2167,18 @@ function createMeshCore(agent) {
2167 // Setup file compression
2168 var ofile = cmd.path + '/' + cmd.output;
2169 this.write(Buffer.from(JSON.stringify({ action: 'dialogmessage', msg: 'zipping' })));
2170 + this.zipfile = ofile;
2171 + delete this.zipcancel;
2172 var out = require('fs').createWriteStream(ofile, { flags: 'wb' });
2173 out.xws = this;
2171 - out.on('close', function () { this.xws.write(Buffer.from(JSON.stringify({ action: 'dialogmessage', msg: null }))); this.xws.write(Buffer.from(JSON.stringify({ action: 'refresh' }))); this.xws.zip = null; });
2174 + out.on('close', function () {
2175 + this.xws.write(Buffer.from(JSON.stringify({ action: 'dialogmessage', msg: null })));
2176 + this.xws.write(Buffer.from(JSON.stringify({ action: 'refresh' })));
2177 + if (this.xws.zipcancel === true) { fs.unlinkSync(this.xws.zipfile); } // Delete the complete file.
2178 + delete this.xws.zipcancel;
2179 + delete this.xws.zipfile;
2180 + delete this.xws.zip;
2181 + });
2182 this.zip = require('zip-writer').write({ files: p, basePath: cmd.path });
2183 this.zip.xws = this;
2184 this.zip.on('progress', require('events').moderated(function (name, p) { this.xws.write(Buffer.from(JSON.stringify({ action: 'dialogmessage', msg: 'zippingFile', file: ((process.platform == 'win32') ? (name.split('/').join('\\')) : name), progress: p }))); }, 2000));
@@ -2176,7 +2186,7 @@ function createMeshCore(agent) {
2186 break;
2187 case 'cancel':
2188 // Cancel zip operation if present
2179 - try { this.zip.cancel(function () { }); } catch (ex) { }
2189 + try { this.zipcancel = true; this.zip.cancel(function () { }); } catch (ex) { }
2190 this.zip = null;
2191 break;
2192 default: