2929// ********************************************************************************************
3030// Note: In NodeJS modules are loaded synchronously and processed in the order they occur
3131// ********************************************************************************************
32- var nconf = require ( 'nconf' ) ; //https://github.com/indexzero/nconf
33- var JSON5 = require ( 'json5' ) ; //https://github.com/aseemk/json5
32+ var fs = require ( 'fs' ) ;
3433var path = require ( 'path' ) ;
3534
36- nconf . argv ( )
37- . env ( )
38- . file ( {
39- file : './settings.json5' ,
40- format : JSON5 ,
41- } ) ;
35+ const config = require ( './config' ) ;
36+
37+ const nconf = config . nconf ;
38+ const coreImagesDir = config . coreImagesDir ;
39+ const userImagesDir = config . userImagesDir ;
40+ const coreMetricsDir = config . coreMetricsDir ;
41+ const userMetricsDir = config . userMetricsDir ;
42+ const dbDir = config . dbDir ;
43+
44+ fs . mkdirSync ( dbDir , { 'recursive' : true } ) ;
45+ fs . mkdirSync ( userImagesDir , { 'recursive' : true } ) ;
4246
4347global . settings = nconf . get ( 'settings' ) ;
4448
@@ -51,10 +55,8 @@ var Datastore = require('nedb'); //https://github
5155var nodemailer = require ( 'nodemailer' ) ; //https://github.com/andris9/Nodemailer
5256var http = require ( 'http' ) ;
5357var url = require ( 'url' ) ;
54- var dbDir = path . resolve ( [ settings . database . directory . value ] ) ;
5558db = new Datastore ( { filename : path . join ( dbDir , settings . database . name . value ) , autoload : true } ) ; //used to keep all node/metric data
5659var dbCompacted = Date . now ( ) ;
57- var fs = require ( 'fs' ) ;
5860var gatewayUptime = '' ;
5961var gatewayFrequency = '' ;
6062global . port = undefined ;
@@ -253,19 +255,42 @@ global.getGraphData = function(nodeId, metricKey, start, end, exportMode) {
253255 return { graphData :graphData , options : graphOptions } ;
254256}
255257
256- global . getNodeIcons = function ( dir , files_ , steps ) {
257- files_ = files_ || [ ] ;
258- dir = dir || __dirname + '/www/images' ;
259- steps = steps || 0 ;
260- var files = fs . readdirSync ( dir ) ;
261- for ( var i in files ) {
262- var name = dir + '/' + files [ i ] ;
263- if ( fs . statSync ( name ) . isDirectory ( ) && steps == 0 ) //recurse 1 level only
264- getNodeIcons ( name , files_ , steps + 1 ) ;
265- else if ( files [ i ] . match ( / ^ i c o n _ .+ \. ( b m p | p n g | j p g | j p e g | i c o ) $ / ig) ) //images only
266- files_ . push ( name . replace ( __dirname + '/www/images/' , '' ) ) ;
258+ global . getNodeIcons = function ( {
259+ dir = coreImagesDir ,
260+ root = null ,
261+ files = [ ] ,
262+ steps = 0 ,
263+ prefix = 'icon_' ,
264+ extensions = [ '.bmp' , '.png' , '.jpg' , '.jpeg' , '.ico' ] ,
265+ } = { } ) {
266+ dir = path . resolve ( dir ) ;
267+ root = path . resolve ( root || dir ) ;
268+
269+ console . log ( { 'dir' : dir , 'root' : root , 'files' : files , 'steps' : steps , 'prefix' : prefix , 'extensions' : extensions } ) ;
270+
271+ var basenames = fs . readdirSync ( dir ) ;
272+ for ( var i in basenames ) {
273+ var basename = basenames [ i ] ;
274+ var file = path . join ( dir , basename ) ;
275+
276+ if ( fs . statSync ( file ) . isDirectory ( ) && steps == 0 ) //recurse 1 level only
277+ getNodeIcons ( { 'dir' : file , 'root' : root , 'files' : files , 'steps' : steps + 1 , 'prefix' : prefix , 'extensions' : extensions } ) ;
278+ else if ( basename . startsWith ( prefix ) && extensions . includes ( path . extname ( file ) ) )
279+ files . push ( path . relative ( root , file ) ) ;
267280 }
268- return files_ ;
281+
282+ return files ;
283+ }
284+
285+ global . allNodeIcons = function ( ) {
286+ var files ;
287+
288+ if ( coreImagesDir == userImagesDir )
289+ files = [ ] ;
290+ else
291+ files = getNodeIcons ( { 'dir' : userImagesDir , 'prefix' : '' } ) ;
292+
293+ return getNodeIcons ( { 'dir' : coreImagesDir , 'files' : files } ) ;
269294}
270295
271296//authorize handshake - make sure the request is proxied from localhost, not from the outside world
@@ -310,7 +335,7 @@ io.sockets.on('connection', function (socket) {
310335 socket . emit ( 'SETTINGSDEF' , settings ) ;
311336 broadcastServerInfo ( socket ) ;
312337 socket . emit ( 'DBCOMPACTED' , dbCompacted ) ;
313- socket . emit ( 'NODEICONS' , getNodeIcons ( ) ) ;
338+ socket . emit ( 'NODEICONS' , allNodeIcons ( ) ) ;
314339
315340 //pull all nodes from the database and send them to client
316341 db . find ( { _id : { $exists : true } } , function ( err , entries ) {
@@ -328,7 +353,7 @@ io.sockets.on('connection', function (socket) {
328353 } ) ;
329354
330355 socket . on ( 'REFRESHICONS' , function ( ) {
331- io . sockets . emit ( 'NODEICONS' , getNodeIcons ( ) ) ;
356+ io . sockets . emit ( 'NODEICONS' , allNodeIcons ( ) ) ;
332357 } ) ;
333358
334359 socket . on ( 'UPDATENODELISTORDER' , function ( newOrder ) {
0 commit comments