DB record expire in config + Mesh folder optimizations

Ylian Saint-Hilaire committed Feb 18, 2019 at 22:20 UTC 9e56a89cc62333da3837d7968b492266c61a349e
5 files changed +31 -9
db.js
+6
@@ -35,6 +35,12 @@ module.exports.CreateDB = function (parent) {
35 obj.identifier = null;
36 obj.dbKey = null;
37
38 + // Read expiration time from configuration file
39 + if (typeof obj.parent.args.dbexpire == 'object') {
40 + if (typeof obj.parent.args.dbexpire.events == 'number') { expireEventsSeconds = obj.parent.args.dbexpire.events; }
41 + if (typeof obj.parent.args.dbexpire.powerevents == 'number') { expirePowerEventsSeconds = obj.parent.args.dbexpire.powerevents; }
42 + }
43 +
44 if (obj.parent.args.mongodb) {
45 // Use MongoDB
46 obj.databaseType = 2;
meshuser.js
+15 -7
@@ -386,7 +386,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
386 case 'files':
387 {
388 // Send the full list of server files to the browser app
389 - if ((user != null) && (user.siteadmin != null) && (user.siteadmin & 8) != 0) { updateUserFiles(user, ws, domain); }
389 + updateUserFiles(user, ws, domain);
390 break;
391 }
392 case 'fileoperation':
@@ -411,6 +411,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
411 }
412 }
413 }
414 +
415 + // If the entire mesh folder is empty, remove it.
416 + // TODO: Should only check this when we deleted something in the mesh root folder.
417 + try {
418 + if (command.path[0].startsWith('mesh//')) {
419 + path = meshPathToRealPath([command.path[0]], user);
420 + obj.fs.readdir(path, function (err, dir) { if ((err == null) && (dir.length == 0)) { obj.fs.rmdir(path, function (err) { }); } });
421 + }
422 + } catch (ex) { }
423 }
424 else if ((command.fileop == 'rename') && (obj.common.IsFilenameValid(command.oldname) == true) && (obj.common.IsFilenameValid(command.newname) == true)) { try { obj.fs.renameSync(path + "/" + command.oldname, path + "/" + command.newname); } catch (e) { } } // Rename
425 else if ((command.fileop == 'copy') || (command.fileop == 'move')) {
@@ -1981,6 +1990,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1990 };
1991
1992 function updateUserFiles(user, ws, domain) {
1993 + if ((user == null) || (user.siteadmin == null) || ((user.siteadmin & 8) == 0)) return;
1994 +
1995 // Request the list of server files
1996 var files = { action: 'files', filetree: { n: 'Root', f: {} } };
1997
@@ -1994,6 +2005,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2005 try {
2006 files.filetree.f[user._id].f = readFilesRec(obj.path.join(obj.parent.filespath, domainx + "/user-" + usersplit[2]));
2007 } catch (e) {
2008 + // TODO: We may want to fake this file structure until it's needed.
2009 // Got an error, try to create all the folders and try again...
2010 try { obj.fs.mkdirSync(obj.parent.filespath); } catch (e) { }
2011 try { obj.fs.mkdirSync(obj.path.join(obj.parent.filespath, domainx)); } catch (e) { }
@@ -2013,13 +2025,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2025
2026 // Read all files recursively
2027 try {
2016 - files.filetree.f[mesh._id].f = readFilesRec(obj.parent.path.join(__dirname, "files/" + domainx + "/mesh-" + meshsplit[2]));
2028 + files.filetree.f[mesh._id].f = readFilesRec(obj.parent.path.join(obj.parent.filespath, domainx + "/mesh-" + meshsplit[2]));
2029 } catch (e) {
2018 - // Got an error, try to create all the folders and try again...
2019 - try { obj.fs.mkdirSync(obj.parent.filespath); } catch (e) { }
2020 - try { obj.fs.mkdirSync(obj.parent.path.join(obj.parent.filespath, domainx)); } catch (e) { }
2021 - try { obj.fs.mkdirSync(obj.parent.path.join(obj.parent.filespath, domainx + "/mesh-" + meshsplit[2])); } catch (e) { }
2022 - try { files.filetree.f[mesh._id].f = readFilesRec(obj.parent.path.join(obj.parent.filespath, domainx + "/mesh-" + meshsplit[2])); } catch (e) { }
2030 + files.filetree.f[mesh._id].f = {}; // Got an error, return empty folder. We will create the folder only when needed.
2031 }
2032 }
2033 }
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.2.8-u",
3 + "version": "0.2.8-v",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
sample-config.json
+1 -1
@@ -2,13 +2,13 @@
2 "__comment__" : "This is a sample configuration file, edit a section and remove the _ in front of the name. Refer to the user's guide for details.",
3 "settings": {
4 "_MongoDb": "mongodb://127.0.0.1:27017/meshcentral",
5 - "_MongoDbCol": "meshcentral",
5 "_WANonly": true,
6 "_LANonly": true,
7 "_Minify": 1,
8 "_SessionTime": 30,
9 "_SessionKey": "MyReallySecretPassword1",
10 "_DbEncryptKey": "MyReallySecretPassword2",
11 + "_DbExpire": { "events": 1728000, "powerevents": 864000 },
12 "_Port": 443,
13 "_RedirPort": 80,
14 "_AllowLoginToken": true,
webserver.js
+8
@@ -1410,6 +1410,14 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1410 // Get total bytes in the path
1411 var totalsize = readTotalFileSize(xfile.fullpath);
1412 if ((xfile.quota == null) || (totalsize < xfile.quota)) { // Check if the quota is not already broken
1413 +
1414 + // See if we need to create the folder
1415 + var domainx = 'domain';
1416 + if (domain.id.length > 0) { domainx = 'domain-' + usersplit[1]; }
1417 + try { obj.fs.mkdirSync(obj.parent.filespath); } catch (e) { }
1418 + try { obj.fs.mkdirSync(obj.parent.path.join(obj.parent.filespath, domainx)); } catch (e) { }
1419 + try { obj.fs.mkdirSync(xfile.fullpath); } catch (e) { }
1420 +
1421 if (fields.name != null) {
1422 // Upload method where all the file data is within the fields.
1423 var names = fields.name[0].split('*'), sizes = fields.size[0].split('*'), types = fields.type[0].split('*'), datas = fields.data[0].split('*');