-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_annotator.js
More file actions
167 lines (155 loc) · 5.11 KB
/
app_annotator.js
File metadata and controls
167 lines (155 loc) · 5.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//This is the server for working on models
//Users on this server have full access to
const port = 4000;
const authentication = "password"; //Must be entered when trying to safe changes on the site
const fileWhitelist = ["models","arrowBack.png","favicon.ico","folderIcon.png", "loading.webp", "script_for_annotator.js", "scripta.js", "scriptf.js","style.css"]
//Whitelist only on the same level, directories that are mentioned here are fully accessible. Only fileindex and index.html are always included
const http = require('http');
const fs = require('fs');
const qs = require('querystring');
//var path = require('path');
var index;
var indexFile = fs.readFile("./fileindex.txt", 'utf-8', function(errori, resindex) {
//console.log(resindex);
index = resindex;
//console.log(index);
});
fs.readFile('./index_annotator.html', function(error, html) {
if (error) throw error;
console.log("Server started: Annotator");
http.createServer((req, res) => {
const { method, url } = req;
var opt = {
host: url.split("?")[0],
param: url.split("?")[1]
};
console.log(method, opt.host);
//When receiving annotations, delete this part when porting to explore3d
if (method === "POST") {
//console.log(opt.param);
var body = '';
var auth = "";
req.on('data', chunk => {
//console.log('A chunk of data has arrived: ', chunk, typeof chunk[0]);
for (n in chunk) {
if (typeof chunk[n] === "number") {
body = body + String.fromCharCode(chunk[n]);
//process.stdout.write(String.fromCharCode(chunk[n]));
}
}
});
req.on('end', () => {
//console.log(body);
path = getAnnoPath(opt.param.substr(2));
if (req.headers["authentication"] === authentication) {// Check authentication
fs.writeFile(path, body.substr(0, body.length-1), (err) => {
if (err) {
console.error(err);
}});
console.log("pushing annotation change successfull")
res.writeHeader(204);
} else {
console.log("pushing annotation change tried but failed");
res.writeHeader(401);
}
res.end();
})
}
//Keep this when porting to explore3d vv
//index.html bzw Home
else if (opt.host === "/") {
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(html);
res.end();
//console.log("Base HTML sent");
return;
}
else if (opt.host === "/fileindex") {
res.writeHeader(200, {"Content-Type": "application/json"});
//console.log(index);
res.write(index);
res.end();
//console.log("Index sent");
return;
}
else if (opt.host.substr(opt.host.length-15) === "annotations.txt") {
//console.log("annotations requested: " + opt.host);
fs.readFile("." + opt.host, "utf-8", (errora, resa)=> {
res.writeHeader(200, {"Content-Type": "application/json"});
//console.log(resa);
res.write(resa);
res.end();
return;
});
}
//Alle anderen Files
else {
var fileEnding = url.split(".")[url.split(".").length -1];
var ctype = ""; //path = ".";
switch (fileEnding) {
case 'js':
ctype = "text/javascript";
break;
case 'css':
ctype = "text/css";
break;
case 'jpg':
//path = "./models";
ctype = "image/jpg";
break;
default:
ctype = "text/html";
}
try {
//console.log("request " + opt.host + " in " + __dirname);
if (fs.existsSync("." + opt.host)) {
if (opt.host.indexOf(".") === -1) {
throw new Error("Client trying to access a folder, would crash");
}
//Check if requested file is on whitelist
var first = opt.host.split("/")[1];
console.log(first);
var isOnWhitelist = false;
for (n in fileWhitelist) {
if (first === fileWhitelist[n]) {
isOnWhitelist = true;
}
}
if (isOnWhitelist) {
//console.log("File found");
var resfile = fs.readFile("." + opt.host, function(errorf, resfile) {
if (errorf) {
console.log(errorf);
//throw errorf;
}
//console.log(ctype + " tried");
res.writeHeader(200, {"Content-Type": ctype});
res.write(resfile);
res.end();
});
} else {
throw new Error("Client requested file is not on whitelist");
}
} else {
console.log("File not found");
res.writeHeader(404);
res.end();
}
} catch (e) {
res.writeHeader(401);
res.end();
console.error(e);
}
}
}).listen(port)
});
function getAnnoPath(path) {
var pathTXT = "";
var ar = path.split("/");
ar[ar.length-1] = "_" + ar[ar.length-1].substr(0, ar[ar.length-1].length-4);
for (n in ar) {
pathTXT = pathTXT + (ar[n] + "/");
}
pathTXT = pathTXT + "annotations.txt";
return pathTXT;
}