Show file sizes

This commit is contained in:
Violet 2024-02-02 20:13:13 +00:00 committed by GitHub
parent 6ad22ecf55
commit 6971adf579
2 changed files with 34 additions and 9 deletions

View file

@ -1,6 +1,6 @@
const express = require("express"), const express = require("express"),
fs = require("fs"), fs = require("fs"),
path = require("path") path = require("path")
var app = express() var app = express()
@ -8,6 +8,28 @@ var PORT = process.env.PORT || 8080
var directory = process.env.FILES_DIR var directory = process.env.FILES_DIR
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];
}
var baseHTML = fs.readFileSync(path.join(__dirname, 'resources/base.html')).toString() var baseHTML = fs.readFileSync(path.join(__dirname, 'resources/base.html')).toString()
var baseStart = baseHTML.substring(0, baseHTML.indexOf('{CONTENT}')) var baseStart = baseHTML.substring(0, baseHTML.indexOf('{CONTENT}'))
var baseEnd = baseHTML.substring(baseHTML.indexOf('{CONTENT}') + 9, baseHTML.length) var baseEnd = baseHTML.substring(baseHTML.indexOf('{CONTENT}') + 9, baseHTML.length)
@ -26,7 +48,7 @@ app.listen(PORT, () => {
app.get("/*", (req, res) => { app.get("/*", (req, res) => {
var file = req.params[0] var file = req.params[0]
var absPath = path.join(directory, file) var absPath = path.join(directory, file)
res.setHeader('Content-Type', 'text/html') res.setHeader('Content-Type', 'text/html')
res.setHeader("X-Accel-Buffering", "no") res.setHeader("X-Accel-Buffering", "no")
@ -49,9 +71,8 @@ app.get("/*", (req, res) => {
for (let index = 0; index < dirContents.length; index++) { for (let index = 0; index < dirContents.length; index++) {
const file = dirContents[index]; const file = dirContents[index];
var fileStats = fs.statSync(path.join(absPath, file)) var fileStats = fs.statSync(path.join(absPath, file))
// console.log(fileStats.isDirectory)
if (!fileStats.isDirectory()) { if (!fileStats.isDirectory()) {
res.write(`<a href="./${file}">${file}</a><br>`) res.write(`<li><a href="./${file}">${file}</a> | ${humanFileSize(fileStats.size)}</li>`)
} else { } else {
dirs.push(file) dirs.push(file)
} }
@ -60,9 +81,11 @@ app.get("/*", (req, res) => {
for (let index = 0; index < dirs.length; index++) { for (let index = 0; index < dirs.length; index++) {
const file = dirs[index]; const file = dirs[index];
res.write(`<a href="./${file}">./${file}/</a><br>`) var fileStats = fs.statSync(path.join(absPath, file))
} res.write(`<li><a href="./${file}">./${file}/</a></li>`)
}
// res.write(`<a href="./${file}">./${file}/</a><br>`) // res.write(`<a href="./${file}">./${file}/</a><br>`)
res.write(baseEnd) res.write(baseEnd)

View file

@ -10,6 +10,8 @@
<title>{TITLE}</title> <title>{TITLE}</title>
</head> </head>
<body> <body>
{CONTENT} <ul>
{CONTENT}
</ul>
</body> </body>
</html> </html>