FileSharer/index.js

140 lines
3.5 KiB
JavaScript
Raw Normal View History

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
var pubDir = path.join(directory, 'public')
2024-09-16 17:13:24 -05:00
var privDir = path.join(directory, 'private')
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-07-16 14:13:14 -05:00
const videoFormats = ["mp4", "mkv", "webm"]
2024-02-05 12:35:10 -06:00
2024-09-16 17:13:24 -05:00
app.use(express.static(privDir))
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-04-23 13:15:38 -05:00
app.get("/download/*", (req, res) => {
var download = req.params[0]
var downloadPath = path.join(directory, download)
var downloadName = downloadPath.substring(downloadPath.lastIndexOf("/"))
if (!fs.existsSync(downloadPath)) {
downloadPath = path.join(pubDir, download)
}
res.setHeader('Content-disposition', `attachment; filename=${downloadName}`);
var stream = fs.createReadStream(downloadPath)
stream.pipe(res)
})
2024-09-16 17:13:24 -05:00
app.get("/watch/*", (req, res) => {
res.redirect("/" + req.params[0])
})
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]
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
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)
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-09-16 17:13:24 -05:00
addedHTML += `<li><a href="./${file}">${file}</a> | ${humanFileSize(fileStats.size)} | <a href="/download/${userPath}">download</a></li>`
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}`,
);
});