diff --git a/index.js b/index.js index d0daac0..a8c282f 100644 --- a/index.js +++ b/index.js @@ -35,12 +35,62 @@ if (!directory) { return } +const videoFormats = ["mp4", "mkv"] + app.use(express.static(directory)) app.listen(PORT, () => { console.log("Now listening on PORT: " + PORT) }) +app.get("/watch/*", (req, res) => { + res.setHeader("Content-Type", "text/html") + + var video = req.params[0] + var absPath = path.join(directory, video) + + var html = fs.readFileSync(path.join(__dirname, 'resources/watch.html')).toString() + + html = html.replaceAll("{VID_PATH}", video) + + var vidTitle = video.substring(video.lastIndexOf('/') + 1, video.lastIndexOf('.')) + + var vidStats = fs.statSync(absPath) + + html = html.replaceAll("{TITLE}", vidTitle) + + html = html.replaceAll("{VID_INFO}", `Size: ${humanFileSize(vidStats.size)}`) + + html = html.replaceAll("{BACK}", "/" + video.substring(0, video.lastIndexOf("/"))) + + res.send(html) +}) + +app.get("/video/*", (req, res) => { + var video = req.params[0] + var vidPath = path.join(directory, video) + + const range = req.headers.range + const videoPath = vidPath; + const videoSize = fs.statSync(videoPath).size + const chunkSize = 1 * 1e6; + const start = Number(range.replace(/\D/g, "")) + const end = Math.min(start + chunkSize, videoSize - 1) + const contentLength = end - start + 1; + const headers = { + "Content-Range": `bytes ${start}-${end}/${videoSize}`, + "Accept-Ranges": "bytes", + "Content-Length": contentLength, + "Content-Type": "video/mkv" + } + res.writeHead(206, headers) + const stream = fs.createReadStream(videoPath, { + start, + end + }) + stream.pipe(res) +}) + app.get("/*", (req, res) => { var file = req.params[0] var absPath = path.join(directory, file) @@ -76,11 +126,22 @@ app.get("/*", (req, res) => { var dirs = [] + var ogFolder = file + for (let index = 0; index < dirContents.length; index++) { const file = dirContents[index]; + var userPath = path.join(ogFolder, file) + var fileStats = fs.statSync(path.join(absPath, file)) + if (!fileStats.isDirectory()) { - res.write(`
{VID_INFO}
+