-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_service.js
102 lines (91 loc) · 3.12 KB
/
folder_service.js
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
var fs = require('fs');
var http = require('http');
//var sys = require('sys');
//var jsdom = require('./node_modules/jsdom/lib/jsdom');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(dataService.init(res, "./source_data", 1) + '');
res.end();
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
// ##### DATA SERVICE #####
dataService={
debugFlag :0,
folderPath:"",
result :"",
init :function( res, folderPath, debugFlag ){
this.res = res;
this.debugFlag = debugFlag;
this.folderPath = folderPath;
if(this.debugFlag == 1 ) console.log(" Initialized..");
return this.generateResponse();
},
generateResponse:function(){
if(this.debugFlag == 1 ) console.log("Fn -- Generate Response..");
try{
stats = fs.lstatSync(this.folderPath);
if(stats.isDirectory())
{
return this.successResponse();
}
}catch(e){
return this.failureResponse(e);
}
},
// to return the failure response
failureResponse:function( msg ){
if(this.debugFlag==1) console.log("Fn -- failureResponse");
var errorJsonResponse = "";
errorJsonResponse = "{\"folderservice\":{\"resultCode\":1,\"resultMsg\":\"Failure"+msg+"\"}}";
return errorJsonResponse;
},
successResponse:function(){
if(this.debugFlag==1) console.log("Fn -- successResponse");
var successJsonResponse="";
try{
successJsonResponse = "{\"folderservice\":{\"resultCode\":0,\"resultMsg\":\"success\"";
filenames = fs.readdirSync(this.folderPath);
filenames.sort();
var tabs_obj = [];
var tabs=[];
successJsonResponse += ",\"tabs\": [ ";
// Reading the Menu(top) Level Folders
for( i = 0; i < filenames.length; i++) {
if(fs.lstatSync(this.folderPath+'/'+filenames[i]).isDirectory()){
// if its a diretory read the second level folder...
successJsonResponse += "{\"menu\":";
successJsonResponse += "\"" +filenames[i] + "\"";
successJsonResponse += ",\"submenus\": [";
var seclevel_filenames = fs.readdirSync(this.folderPath+'/'+filenames[i]);
seclevel_filenames.sort();
var tmp_obj = "";
for (var j = 0; j < seclevel_filenames.length; j++){
tmp_obj += "{"+ "\"linkText \": \""+ seclevel_filenames[j]+"\"} ,";
//console.log( "{"+ "\"linkText \": \""+ seclevel_filenames[j]+"\"} ,");
}
tmp_obj = tmp_obj.substring(0, tmp_obj.length - 1);
successJsonResponse += tmp_obj;
successJsonResponse += "]";
successJsonResponse += "},";
//if( i!=filenames.length-1) { successJsonResponse+=","; }
}
} // end for
successJsonResponse = successJsonResponse.substring(0, successJsonResponse.length - 1);
successJsonResponse += "]";
successJsonResponse += "}}";
return successJsonResponse;
}catch(err){
console.log( err);
//this.failureResponse(err);
}
}, // Reading the sub menus( dropdown) Level..
arrToObj:function(arr){
if(this.debugFlag) console.log("Fn -- arrToObj ");
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
console.log(rv);
return rv;
}
}; // End DataService.