-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathklabServer.js
More file actions
137 lines (132 loc) · 4.08 KB
/
klabServer.js
File metadata and controls
137 lines (132 loc) · 4.08 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
135
136
var http = require('http');
var Url = require('url');
var fs = require('fs');
var Path = require('path');
require(Path.resolve("./ILAB/Modules/Utils.js"));
var Files = require(Path.resolve("./ILAB/Modules/Files.js"));
KLabServer = function(config, router, logger){
this.Config = config;
this.noCollectData = true;
var filesRouter = Files(config, this);
router.for("Main","/>", {
GET : function(context){
if (context.query["action"] == "edit" || context.query["action"] == "create"){
context.res.setHeader("Content-Type", "text/html; charset=utf-8");
if (context.query["action"] == "create"){
var path = context.pathName;
if (config.basepath){
path = config.basepath + context.pathName;
}
path = Path.resolve(path);
if (!fs.existsSync(path)){
fs.writeFileSync(path, "", null);
};
}
fs.readFile(Path.resolve("./Klab/TextEditor.htm"), "utf8", function(err, result){
if (err){
context.finish(500, "Not found files view page " + err);
return;
}
context.finish(200, result);
});
return false;
}
var path = context.pathName;
if (config.basepath){
path = config.basepath + context.pathName;
}
if (path.indexOf(".") != 0){
path = "." + path;
}
path = Path.resolve(path);
fs.stat(path, function(err, stat){
if (err){
if (context.query["action"] == "file"){
fs.writeFile(path, null, null, function(err, result){
context.res.setHeader("Content-Type", "text/plain; charset=utf-8");
context.finish(200, result);
});
return;
}
if(context.query["action"] == "dir"){
fs.mkdir(path, null, function(err, result){
context.res.setHeader("Content-Type", "text/plain; charset=utf-8");
context.finish(200, result);
});
return;
}
context.continue();
return;
}
if (stat.isDirectory()){
context.res.setHeader("Content-Type", "text/html; charset=utf-8");
fs.readFile(Path.resolve("./Klab/files.htm"), "utf8", function(err, result){
if (err){
context.finish(500, "Not found files view page " + err);
return;
}
context.finish(200, result);
});
return;
}
if (stat.isFile()){
}
context.continue();
});
return false;
},
POST : function(context){
if (context.completed) return true;
var path = context.pathName;
if (config.basepath){
path = config.basepath + context.pathName;
}
if (path.indexOf(".") != 0){
path = "." + path;
}
path = Path.resolve(path);
logger.log("Writing " + path);
var writeable = fs.createWriteStream(Path.resolve(path),{'flags': 'w', 'encoding': 'binary'});
if (context.data) console.error("DATA DETECTED!");
context.req.on("data", function(data){
writeable.write(data);
});
context.req.on("end", function(){
context.finish(200);
});
return false;
},
DELETE : function(context){
if (context.completed) return true;
var path = context.pathName;
if (config.basepath){
path = config.basepath + context.pathName;
}
if (path.indexOf(".") != 0){
path = "." + path;
}
path = Path.resolve(path);
fs.exists(path, function(exists){
if (!exists){
context.finish(404, "file " + path + " not found");
return;
}
info("Deleting " + path);
fs.unlink(path, function(err, result){
if (err){
Channels.emit("/file-system/action.delete.error", path,err);
context.finish(500, "Delete error " + path + " " + err);
return;
}
Channels.emit("/file-system/action.delete", path);
context.finish(200, "Deleted " + path);
});
});
return false;
}
});
router.for("Main","/<", filesRouter);
};
module.exports = function(config, router, r2, logger){
return new KLabServer(config, router, logger);
}