Improved agent upload and download events.
Ylian Saint-Hilaire committed
Jan 24, 2021 at 22:46 UTC
d849d6cd07164264a7b23ca93dac7d8868c290fb
2 files changed
+11
-3
agents/meshcore.js
+8
-2
@@ -1496,7 +1496,7 @@ function onTunnelClosed() {
1496
*/
1497
1498
// If there is a upload or download active on this connection, close the file
1499
- if (this.httprequest.uploadFile) { fs.closeSync(this.httprequest.uploadFile); delete this.httprequest.uploadFile; delete this.httprequest.uploadFileid; delete this.httprequest.uploadFilePath; }
1499
+ if (this.httprequest.uploadFile) { fs.closeSync(this.httprequest.uploadFile); delete this.httprequest.uploadFile; delete this.httprequest.uploadFileid; delete this.httprequest.uploadFilePath; delete this.httprequest.uploadFileSize; }
1500
if (this.httprequest.downloadFile) { delete this.httprequest.downloadFile; }
1501
1502
// Clean up WebRTC
@@ -1523,9 +1523,11 @@ function onTunnelData(data) {
1523
// Save the data to file being uploaded.
1524
if (data[0] == 0) {
1525
// If data starts with zero, skip the first byte. This is used to escape binary file data from JSON.
1526
+ this.httprequest.uploadFileSize += (data.length - 1);
1527
try { fs.writeSync(this.httprequest.uploadFile, data, 1, data.length - 1); } catch (e) { sendConsoleText('FileUpload Error'); this.write(Buffer.from(JSON.stringify({ action: 'uploaderror' }))); return; } // Write to the file, if there is a problem, error out.
1528
} else {
1529
// If data does not start with zero, save as-is.
1530
+ this.httprequest.uploadFileSize += data.length;
1531
try { fs.writeSync(this.httprequest.uploadFile, data); } catch (e) { sendConsoleText('FileUpload Error'); this.write(Buffer.from(JSON.stringify({ action: 'uploaderror' }))); return; } // Write to the file, if there is a problem, error out.
1532
}
1533
this.write(Buffer.from(JSON.stringify({ action: 'uploadack', reqid: this.httprequest.uploadFileid }))); // Ask for more data.
@@ -1552,6 +1554,7 @@ function onTunnelData(data) {
1554
try { stats = require('fs').statSync(this.httprequest.xoptions.file) } catch (e) { }
1555
try { if (stats) { this.httprequest.downloadFile = fs.createReadStream(this.httprequest.xoptions.file, { flags: 'rbN' }); } } catch (e) { }
1556
if (this.httprequest.downloadFile) {
1557
+ MeshServerLogEx(106, [this.httprequest.xoptions.file, stats.size], 'Download: \"' + this.httprequest.xoptions.file + '\", Size: ' + stats.size, this.httprequest);
1558
//sendConsoleText('BasicFileTransfer, ok, ' + this.httprequest.xoptions.file + ', ' + JSON.stringify(stats));
1559
this.write(JSON.stringify({ op: 'ok', size: stats.size }));
1560
this.httprequest.downloadFile.pipe(this);
@@ -2292,7 +2295,7 @@ function onTunnelData(data) {
2295
if (cmd.path == undefined) break;
2296
var filepath = cmd.name ? obj.path.join(cmd.path, cmd.name) : cmd.path;
2297
this.httprequest.uploadFilePath = filepath;
2295
- MeshServerLogEx(50, [filepath], 'Upload: \"' + filepath + '\"', this.httprequest);
2298
+ this.httprequest.uploadFileSize = 0;
2299
try { this.httprequest.uploadFile = fs.openSync(filepath, 'wbN'); } catch (e) { this.write(Buffer.from(JSON.stringify({ action: 'uploaderror', reqid: cmd.reqid }))); break; }
2300
this.httprequest.uploadFileid = cmd.reqid;
2301
if (this.httprequest.uploadFile) { this.write(Buffer.from(JSON.stringify({ action: 'uploadstart', reqid: this.httprequest.uploadFileid }))); }
@@ -2302,11 +2305,13 @@ function onTunnelData(data) {
2305
{
2306
// Indicates that an upload is done
2307
if (this.httprequest.uploadFile) {
2308
+ MeshServerLogEx(105, [this.httprequest.uploadFilePath, this.httprequest.uploadFileSize], 'Upload: \"' + this.httprequest.uploadFilePath + '\", Size: ' + this.httprequest.uploadFileSize, this.httprequest);
2309
fs.closeSync(this.httprequest.uploadFile);
2310
this.write(Buffer.from(JSON.stringify({ action: 'uploaddone', reqid: this.httprequest.uploadFileid }))); // Indicate that we closed the file.
2311
delete this.httprequest.uploadFile;
2312
delete this.httprequest.uploadFileid;
2313
delete this.httprequest.uploadFilePath;
2314
+ delete this.httprequest.uploadFileSize;
2315
}
2316
break;
2317
}
@@ -2320,6 +2325,7 @@ function onTunnelData(data) {
2325
delete this.httprequest.uploadFile;
2326
delete this.httprequest.uploadFileid;
2327
delete this.httprequest.uploadFilePath;
2328
+ delete this.httprequest.uploadFileSize;
2329
}
2330
break;
2331
}
views/default.handlebars
+3
-1
@@ -11364,7 +11364,9 @@
11364
101: "Added device share {0} from {1} to {2}",
11365
102: "Removed device share {0}",
11366
103: "Batch upload of {0} file(s) to folder {1}",
11367
- 104: "Automated download of agent core dump file: \"{0}\""
11367
+ 104: "Automated download of agent core dump file: \"{0}\"",
11368
+ 105: "Upload: \"{0}\", Size: {1}",
11369
+ 106: "Download: \"{0}\", Size: {1}"
11370
};
11371
11372
// Highlights the device being hovered