Show file sizes
This commit is contained in:
parent
6ad22ecf55
commit
6971adf579
2 changed files with 34 additions and 9 deletions
33
index.js
33
index.js
|
@ -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)
|
||||||
|
@ -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,7 +81,9 @@ 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>`)
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
<title>{TITLE}</title>
|
<title>{TITLE}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{CONTENT}
|
<ul>
|
||||||
|
{CONTENT}
|
||||||
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue