-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (130 loc) · 4.08 KB
/
app.js
File metadata and controls
142 lines (130 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
137
138
139
140
141
142
const http = require('http');
const fs = require('fs');
const qs = require('querystring');
//var path = require('path');
var index;
const port = 3000;
const fileWhitelist = ["models","arrowBack.png","favicon.ico","folderIcon.png","fileindex.txt","loading.webp", "script.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
var indexFile = fs.readFile("./fileindex.txt", 'utf-8', function(errori, resindex) {
//console.log(resindex);
index = resindex;
//console.log(index);
});
fs.readFile('./index.html', function(error, html) {
if (error) throw error;
console.log("Server started: Explorer");
http.createServer((req, res) => {
const { method, url } = req;
var opt = {
host: url.split("?")[0],
param: url.split("?")[1]
};
//console.log(method, opt.host);
//index.html bzw Home
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);
try {
if (!fs.existsSync("." + opt.host)) {
throw new Error("Trying to access not existing annotations");
}
fs.readFile("." + opt.host, "utf-8", (errora, resa)=> {
res.writeHeader(200, {"Content-Type": "application/json"});
//console.log(resa, errora);
res.write(resa);
res.end();
return;
});
} catch (e) {
res.writeHeader(404);
res.end();
}
}
//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;
//case 'glb':
//case undefined:
//path = "./models";
//opt.host = opt.host.concat(".glb");
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) {
throw new Error("Client requested file is not on whitelist: " + first);
}
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 {
console.log("File not found");
res.writeHeader(404);
res.end();
}
} catch (e) {
res.writeHeader(404);
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;
}