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

@ -8,6 +8,28 @@ var PORT = process.env.PORT || 8080
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 baseStart = baseHTML.substring(0, baseHTML.indexOf('{CONTENT}'))
var baseEnd = baseHTML.substring(baseHTML.indexOf('{CONTENT}') + 9, baseHTML.length)
@ -49,9 +71,8 @@ app.get("/*", (req, res) => {
for (let index = 0; index < dirContents.length; index++) {
const file = dirContents[index];
var fileStats = fs.statSync(path.join(absPath, file))
// console.log(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 {
dirs.push(file)
}
@ -60,7 +81,9 @@ app.get("/*", (req, res) => {
for (let index = 0; index < dirs.length; 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>`)

View file

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