2024-01-12 12:07:22 -06:00
|
|
|
const express = require("express"),
|
2024-02-02 14:13:13 -06:00
|
|
|
fs = require("fs"),
|
|
|
|
path = require("path")
|
2024-01-12 12:11:52 -06:00
|
|
|
|
|
|
|
var app = express()
|
|
|
|
|
|
|
|
var PORT = process.env.PORT || 8080
|
|
|
|
|
2024-01-16 12:27:26 -06:00
|
|
|
var directory = process.env.FILES_DIR
|
2024-04-17 08:39:49 -05:00
|
|
|
var pubDir = path.join(directory, 'public')
|
2024-01-16 12:27:26 -06:00
|
|
|
|
2024-02-02 14:13:13 -06:00
|
|
|
function humanFileSize(bytes, si = false, dp = 1) {
|
|
|
|
const thresh = si ? 1000 : 1024;
|
|
|
|
|
|
|
|
if (Math.abs(bytes) < thresh) {
|
|
|
|
return bytes + ' B';
|
|
|
|
}
|
|
|
|
|
|
|
|
const units = si
|
|
|
|
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
|
|
let u = -1;
|
|
|
|
const r = 10 ** dp;
|
|
|
|
|
|
|
|
do {
|
|
|
|
bytes /= thresh;
|
|
|
|
++u;
|
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
|
|
|
|
|
|
|
|
|
|
return bytes.toFixed(dp) + ' ' + units[u];
|
|
|
|
}
|
|
|
|
|
2024-01-16 12:27:26 -06:00
|
|
|
if (!directory) {
|
|
|
|
console.error("No directory specified! Please specify one using the environment variable FILES_DIR.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:35:10 -06:00
|
|
|
const videoFormats = ["mp4", "mkv"]
|
|
|
|
|
2024-01-19 12:43:17 -06:00
|
|
|
app.use(express.static(directory))
|
2024-04-17 08:39:49 -05:00
|
|
|
app.use(express.static(pubDir))
|
2024-04-23 11:13:46 -05:00
|
|
|
app.use(express.static(path.join(__dirname, 'resources')))
|
2024-01-16 14:00:06 -06:00
|
|
|
|
2024-01-12 12:11:52 -06:00
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log("Now listening on PORT: " + PORT)
|
|
|
|
})
|
|
|
|
|
2024-02-05 12:35:10 -06:00
|
|
|
app.get("/watch/*", (req, res) => {
|
|
|
|
res.setHeader("Content-Type", "text/html")
|
|
|
|
|
|
|
|
var video = req.params[0]
|
|
|
|
var absPath = path.join(directory, video)
|
2024-04-17 08:44:46 -05:00
|
|
|
if (!fs.existsSync(absPath)) {
|
|
|
|
absPath = path.join(pubDir, video)
|
|
|
|
}
|
2024-02-05 12:35:10 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-04-17 08:44:46 -05:00
|
|
|
if (!fs.existsSync(vidPath)) {
|
|
|
|
vidPath = path.join(pubDir, video)
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:35:10 -06:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2024-01-19 12:43:17 -06:00
|
|
|
app.get("/*", (req, res) => {
|
2024-01-16 12:27:26 -06:00
|
|
|
var file = req.params[0]
|
2024-04-17 08:39:49 -05:00
|
|
|
var absPath = path.join(pubDir, file)
|
2024-02-02 14:13:13 -06:00
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
var html = fs.readFileSync(path.join(__dirname, 'resources/base.html')).toString()
|
2024-02-02 15:23:01 -06:00
|
|
|
|
2024-01-16 12:46:34 -06:00
|
|
|
res.setHeader('Content-Type', 'text/html')
|
2024-04-23 13:05:34 -05:00
|
|
|
|
|
|
|
var addedHTML = ""
|
2024-01-16 13:02:26 -06:00
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
html = html.replace("{TITLE}", '/' + file)
|
2024-02-02 14:08:21 -06:00
|
|
|
|
2024-01-16 12:27:26 -06:00
|
|
|
try {
|
|
|
|
var dirContents = fs.readdirSync(absPath)
|
2024-02-02 15:23:01 -06:00
|
|
|
var dirStats = fs.statSync(absPath)
|
2024-01-16 12:27:26 -06:00
|
|
|
} catch (error) {
|
2024-04-23 13:05:34 -05:00
|
|
|
html = html.replace("{CONTENT}", "<h3>404: not found!</h3><a href='/'>Go to root</a>")
|
|
|
|
res.send(html)
|
2024-01-16 12:36:23 -06:00
|
|
|
return
|
2024-01-16 12:27:26 -06:00
|
|
|
}
|
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += `<h3>${dirContents.length} Files</h3>`.trim()
|
2024-02-02 15:23:01 -06:00
|
|
|
|
2024-01-19 12:46:58 -06:00
|
|
|
if (file != '') {
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += '<a href="../">Parent Directory</a><br>'
|
2024-01-19 12:46:58 -06:00
|
|
|
}
|
2024-01-16 12:27:26 -06:00
|
|
|
|
2024-02-02 14:08:21 -06:00
|
|
|
var dirs = []
|
|
|
|
|
2024-02-05 12:35:10 -06:00
|
|
|
var ogFolder = file
|
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += "<ul>"
|
|
|
|
|
2024-01-16 12:27:26 -06:00
|
|
|
for (let index = 0; index < dirContents.length; index++) {
|
|
|
|
const file = dirContents[index];
|
2024-02-05 12:35:10 -06:00
|
|
|
var userPath = path.join(ogFolder, file)
|
|
|
|
|
2024-02-02 14:08:21 -06:00
|
|
|
var fileStats = fs.statSync(path.join(absPath, file))
|
2024-02-05 12:35:10 -06:00
|
|
|
|
2024-02-02 14:08:21 -06:00
|
|
|
if (!fileStats.isDirectory()) {
|
2024-02-05 12:35:10 -06:00
|
|
|
var fileExtension = file.substring(file.lastIndexOf('.') + 1, )
|
|
|
|
if (videoFormats.includes(fileExtension)) {
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += `<li><a href="/watch/${userPath}">${file}</a> | ${humanFileSize(fileStats.size)}</li>`
|
2024-02-05 12:35:10 -06:00
|
|
|
} else {
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += `<li><a href="./${file}">${file}</a> | ${humanFileSize(fileStats.size)}</li>`
|
2024-02-05 12:35:10 -06:00
|
|
|
}
|
|
|
|
|
2024-02-02 14:08:21 -06:00
|
|
|
} else {
|
|
|
|
dirs.push(file)
|
|
|
|
}
|
2024-01-16 12:27:26 -06:00
|
|
|
}
|
|
|
|
|
2024-02-02 14:08:21 -06:00
|
|
|
for (let index = 0; index < dirs.length; index++) {
|
|
|
|
const file = dirs[index];
|
|
|
|
|
2024-02-02 14:13:13 -06:00
|
|
|
var fileStats = fs.statSync(path.join(absPath, file))
|
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += `<li><a href="./${file}">./${file}/</a></li>`
|
2024-02-02 14:13:13 -06:00
|
|
|
|
|
|
|
}
|
2024-02-02 14:08:21 -06:00
|
|
|
// res.write(`<a href="./${file}">./${file}/</a><br>`)
|
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
addedHTML += "</ul>"
|
|
|
|
|
|
|
|
html = html.replace("{CONTENT}", addedHTML)
|
2024-01-16 12:27:26 -06:00
|
|
|
|
2024-04-23 13:05:34 -05:00
|
|
|
res.write(html, () => {
|
|
|
|
res.end()
|
|
|
|
})
|
2024-02-05 17:04:00 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err, origin) => {
|
|
|
|
fs.writeSync(
|
|
|
|
process.stderr.fd,
|
|
|
|
`Caught exception: ${err}\n` +
|
|
|
|
`Exception origin: ${origin}`,
|
|
|
|
);
|
|
|
|
});
|