-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
135 lines (109 loc) · 4.47 KB
/
server.js
File metadata and controls
135 lines (109 loc) · 4.47 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
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
// Middleware to log requests and responses
app.use((req, res, next) => {
console.log(`Request: ${req.method} ${req.url}`);
const originalSend = res.send;
res.send = function (body) {
console.log(`Response: ${res.statusCode} ${body}`);
originalSend.call(this, body);
};
next();
});
// Corrected sanitizePath function
function sanitizePath(input) {
return path.normalize(input).replace(/^\.\//, "").replace(/\//g, path.sep);
}
// Sanitize and validate folder and episode parameters
function isValidPath(base, target) {
const resolvedBase = path.resolve(base);
const resolvedTarget = path.resolve(base, target);
return resolvedTarget.startsWith(resolvedBase);
}
// Route to serve the index.html file
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
// Updated route to fetch episodes in a specific folder
app.get("/content/:folder/episodes", function (req, res) {
const folder = sanitizePath(decodeURIComponent(req.params.folder));
const contentDir = path.join(__dirname, "content", folder);
if (!isValidPath(path.join(__dirname, "content"), contentDir) || !fs.existsSync(contentDir)) {
console.error("Invalid or non-existent folder:", contentDir);
return res.status(404).send("Folder not found");
}
const episodes = fs.readdirSync(contentDir, { withFileTypes: true })
.filter((dirent) => dirent.isFile() && dirent.name.match(/^[0-9]+\.mp4$/))
.map((dirent) => dirent.name);
console.log("Episodes found:", episodes);
res.json(episodes);
});
// Updated route to fetch a specific episode
app.get("/content/:folder/:episode", function (req, res) {
const folder = sanitizePath(req.params.folder);
const episode = sanitizePath(req.params.episode);
const videoPath = path.join(__dirname, "content", folder, episode);
if (!isValidPath(path.join(__dirname, "content"), videoPath) || !fs.existsSync(videoPath)) {
console.error("Invalid or non-existent file:", videoPath);
return res.status(404).send("File not found");
}
const fileExtension = path.extname(videoPath);
// Only handle video files with .mp4 extension for streaming
if (fileExtension === ".mp4") {
const videoSize = fs.statSync(videoPath).size;
const range = req.headers.range;
if (!range) {
return res.status(400).send("Requires Range header");
}
const CHUNK_SIZE = 10 ** 6; // 1MB
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "video/mp4",
};
res.writeHead(206, headers);
const videoStream = fs.createReadStream(videoPath, { start, end });
videoStream.pipe(res);
} else {
res.sendFile(videoPath);
}
});
// Route to fetch content metadata
app.get("/content", function (req, res) {
const contentDir = path.join(__dirname, "content");
const folders = fs.readdirSync(contentDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const metadata = folders.map((folder) => {
const folderPath = path.join(contentDir, folder);
const coverFile = fs.readdirSync(folderPath, { withFileTypes: true })
.find((dirent) => dirent.isFile() && dirent.name.match(/(cover).(webp|jpg|jpeg|png|gif)$/i));
const coverPath = coverFile ? `/content/${folder}/${coverFile.name}` : null;
return {
title: folder,
cover: coverPath,
};
});
res.json(metadata);
});
app.get("/content/:folder", function (req, res) {
const folder = req.params.folder;
const contentDir = path.join(__dirname, "content", folder);
if (!fs.existsSync(contentDir)) {
return res.status(404).send("Folder not found");
}
res.sendFile(path.join(__dirname, "index.html"));
});
// Static file middleware for the root directory
app.use(express.static(__dirname));
// Static file middleware
//app.use("/content", express.static(path.join(__dirname, "content")));
app.listen(8000, function () {
console.log("Listening on port 8000!");
});