-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (107 loc) · 3.22 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
const express = require("express");
const app = express();
const path = require("path");
const fs = require("fs");
const config = require("./config.json");
const rootPath = config.rootPath;
const port = config.port;
app.get("/dir", (req, res) => {
const providedPath = req.query.path;
let rootPath = config.rootPath;
if (providedPath) {
rootPath = path.join(rootPath, providedPath);
}
const dirList = [];
fs.readdir(rootPath, function (err, files) {
if (err) {
return console.log("FS Error: " + err);
}
files.forEach(function (file) {
if (file[0] === ".") return; // ignore hidden files.
const filePath = path.join(rootPath, file);
// Relative path from the root.
const relPath = path.join(providedPath || "", file);
dirList.push({
file,
relPath,
isDir: fs.statSync(filePath).isDirectory(),
});
});
res.send(dirList);
});
});
// Get the image data providing a relative path.
app.get("/image", (req, res) => {
const imagePath = path.join(rootPath, req.query.path);
fs.access(imagePath, fs.constants.F_OK, (err) => {
if (err) {
return res.status(404).send("Image not found");
}
res.sendFile(imagePath, (err) => {
if (err) {
res.status(500).send("Error getting image data");
}
});
});
});
// Get the file data providing a relative path.
app.get("/subtitles", (req, res) => {
const filePath = path.join(rootPath, req.query.path);
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
return res.status(404).send("Subtitles file not found");
}
res.sendFile(filePath, (err) => {
if (err) {
res.status(500).send("Error getting subtitles file data");
}
});
});
});
// Stream video files (this function was generated by ChatGPT)
app.get("/video", (req, res) => {
const videoPath = path.join(rootPath, req.query.path);
fs.access(videoPath, fs.constants.F_OK, (err) => {
if (err) {
console.log("err: ", err);
return res.status(404).send("Video not found");
}
const stat = fs.statSync(videoPath);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
if (start >= fileSize) {
res
.status(416)
.send(
"Requested range not satisfiable\n" + start + " >= " + fileSize
);
return;
}
const chunkSize = end - start + 1;
const file = fs.createReadStream(videoPath, { start, end });
const head = {
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunkSize,
"Content-Type": "video/mp4",
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
"Content-Length": fileSize,
"Content-Type": "video/mp4",
};
res.writeHead(200, head);
fs.createReadStream(videoPath).pipe(res);
}
});
});
app.use("/", express.static(__dirname + "/public"));
app.listen(port, function () {
console.log("Video server running on " + port + "...");
});