-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (77 loc) · 2.01 KB
/
index.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
const port = process.env.port || 5000;
const express = require("express");
const path = require("path");
const app = express();
const cp = require("child_process");
const server = require("http").createServer(app);
const fs = require("fs");
var types = JSON.parse(fs.readFileSync("types.json", "utf-8"));
//Pass json in fetch()
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
app.use("/f/", (req, res) => {
//Send file
res.sendFile(
path
.resolve("C:\\" + decodeURI(req.url).replace(/\//g, "\\"))
.replace(/"/g, "")
);
});
function getType(str) {
for (const key in types) {
if (types[key].findIndex((f) => str.includes(f)) > -1) return key;
}
return;
}
function get(res) {
/*
Send Different types of html by type of file
if is folder send list of files folder
*/
switch (getType()) {
case "video":
res.sendFile(path.resolve("public/type/video.html"));
break;
//To do: place others types
default:
res.sendFile(path.resolve("public/folder.html"));
break;
}
}
function post(req, res) {
//return list with files in Folder
let location = String(req.body.l).substr(1, String(req.body.l).length);
location = location.replace(/\//g, "\\");
location = decodeURI(location);
console.log(location);
switch (req.body.type) {
case "index":
let a = cp.exec("dir /b " + location);
let result = [];
a.stdout.on("data", (chunk) => {
String(chunk)
.split("\n")
.forEach((f) => {
result.push(f);
});
});
a.stdout.on("close", () => {
result = result.filter((f) => f != "");
res.send(JSON.stringify(result));
});
break;
case "video":
break;
default:
res.status("404");
break;
}
}
app.use("/", async (req, res) => {
if (req.method == "GET") get(req, res);
else if (req.method == "POST") post(req, res);
});
server.listen(port, () => {
console.log("Server is port: " + port);
});