-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdb.js
More file actions
134 lines (124 loc) · 5.49 KB
/
db.js
File metadata and controls
134 lines (124 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @description MeshCentral-FileDistribution database module
* @author Ryan Blenis
* @copyright Ryan Blenis 2019
* @license Apache-2.0
*/
"use strict";
var Datastore = null;
var formatId = null;
module.exports.CreateDB = function(meshserver) {
var obj = {};
var NEMongo = require(__dirname + '/nemongo.js');
module.paths.push(require('path').join(meshserver.parentpath, 'node_modules')); // we need to push the node_modules folder for nedb
obj.dbVersion = 1;
obj.initFunctions = function () {
obj.updateDBVersion = function(new_version) {
return obj.fdFile.updateOne({type: "db_version"}, { $set: {version: new_version} }, {upsert: true});
};
obj.getDBVersion = function() {
return new Promise(function(resolve, reject) {
obj.fdFile.find( { type: "db_version" } ).project( { _id: 0, version: 1 } ).toArray(function(err, vers){
if (vers.length == 0) resolve(1);
else resolve(vers[0]['version']);
});
});
};
obj.getFileMapsForNode = function(node) {
return obj.fdFile.find(
{ type: 'map', node: node }
).sort(
{ serverpath: 1, clientpath: 1 }
).project(
{ serverpath: 1, clientpath: 1, filesize: 1 }
).toArray();
};
obj.findFileForNode = function(node, cpath) {
return obj.fdFile.find(
{ type: 'map', node: node, clientpath: cpath }
).toArray();
};
obj.getServerFiles = function() {
return obj.fdFile.find(
{ type: 'map' }
).toArray();
};
obj.addFileMap = function (currentNodeId, spath, cpath, filesize) {
return obj.fdFile.insertOne({
type: 'map',
node: currentNodeId,
serverpath: spath,
clientpath: cpath,
filesize: filesize
});
};
obj.getNodesForServerPath = function(serverpath, nodeScope) {
if (nodeScope == null || !Array.isArray(nodeScope)) {
nodeScope = [];
}
return obj.fdFile.find(
{ type: 'map', serverpath: serverpath, node: { $in: nodeScope } }
).toArray();
};
obj.update = function(id, args) {
id = formatId(id);
return obj.fdFile.updateOne( { _id: id }, { $set: args } );
};
obj.updateMany = function(where, args) {
return obj.fdFile.updateMany( where, { $set: args } );
};
obj.delete = function(id) {
id = formatId(id);
return obj.fdFile.deleteOne( { _id: id } );
};
obj.get = function(id) {
if (id == null || id == 'null') return new Promise(function(resolve, reject) { resolve([]); });
id = formatId(id);
return obj.fdFile.find( { _id: id } ).toArray();
};
};
if (meshserver.args.mongodb) { // use MongDB
require('mongodb').MongoClient.connect(meshserver.args.mongodb, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {
if (err != null) { console.log("Unable to connect to database: " + err); process.exit(); return; }
var dbname = 'meshcentral';
if (meshserver.args.mongodbname) { dbname = meshserver.args.mongodbname; }
const db = client.db(dbname);
obj.fdFile = db.collection('plugin_filedistribution');
obj.fdFile.indexes(function (err, indexes) {
// Check if we need to reset indexes
var indexesByName = {}, indexCount = 0;
for (var i in indexes) { indexesByName[indexes[i].name] = indexes[i]; indexCount++; }
if ((indexCount != 3) || (indexesByName['Node1'] == null) || (indexesByName['ServerPath1'] == null)) {
// Reset all indexes
console.log('Resetting plugin (FileDistribution) indexes...');
obj.fdFile.dropIndexes(function (err) {
obj.fdFile.createIndex({ serverpath: 1 }, { name: 'ServerPath1' });
obj.fdFile.createIndex({ node: 1 }, { name: 'Node1' });
});
}
});
if (typeof require('mongodb').ObjectID == 'function') {
formatId = require('mongodb').ObjectID;
} else {
formatId = require('mongodb').ObjectId;
}
obj.initFunctions();
});
} else { // use NeDb
try { Datastore = require('@seald-io/nedb'); } catch (ex) { } // This is the NeDB with Node 23 support.
if (Datastore == null) {
try { Datastore = require('@yetzt/nedb'); } catch (ex) { } // This is the NeDB with fixed security dependencies.
if (Datastore == null) { Datastore = require('nedb'); } // So not to break any existing installations, if the old NeDB is present, use it.
}
if (obj.fdFilex == null) {
obj.fdFilex = new Datastore({ filename: meshserver.getConfigFilePath('plugin-filedistribution.db'), autoload: true });
obj.fdFilex.setAutocompactionInterval(40000);
obj.fdFilex.ensureIndex({ fieldName: 'serverpath' });
obj.fdFilex.ensureIndex({ fieldName: 'node' });
}
obj.fdFile = new NEMongo(obj.fdFilex);
formatId = function(id) { return id; };
obj.initFunctions();
}
return obj;
}