This commit is contained in:
tezlm 2022-03-29 17:51:12 -07:00
parent 26b7c8e3a3
commit 4dac540f62
2 changed files with 50 additions and 41 deletions

View file

@ -32,14 +32,18 @@ exports.render = (where, name) => {
}
for(let file of names) {
const stat = fs.statSync(path.join(where, file));
if(stat.isDirectory()) file = file + "/";
files.push({
name: file,
link: file,
date: stat.mtime.toISOString().slice(0, -5).replace("T", " "),
size: stat.isDirectory() ? "dir" : fmtSize(stat.size),
});
try {
const stat = fs.statSync(path.join(where, file));
if(stat.isDirectory()) file = file + "/";
files.push({
name: file,
link: file,
date: stat.mtime.toISOString().slice(0, -5).replace("T", " "),
size: stat.isDirectory() ? "dir" : fmtSize(stat.size),
});
} catch {
continue;
}
}
const sorted = files.sort((a, b) =>

View file

@ -24,43 +24,48 @@ function generate(file, raw = false) {
return send("text/html", mustache.render(template404, { file, time, cache: { style: version("/assets/style.css") } }), 404);
}
const stat = fs.statSync(where);
try {
if(stat.isDirectory()) {
if(!file.endsWith("/")) return (req, res) => res.redirect(file + "/");
return send("text/html", run(handlers.directory));
} else if(path.extname(where) === ".md" && !raw) {
return send("text/html", run(handlers.markdown));
} else if(path.extname(where) === ".gmi" && !raw) {
return send("text/html", run(handlers.gemini));
} else {
const type = types.get(path.extname(where).slice(1));
if(stat.size < 1024 * 1024 * 1) {
return send(type, run(handlers.file));
const stat = fs.statSync(where);
if(stat.isDirectory()) {
if(!file.endsWith("/")) return (req, res) => res.redirect(file + "/");
return send("text/html", run(handlers.directory));
} else if(path.extname(where) === ".md" && !raw) {
return send("text/html", run(handlers.markdown));
} else if(path.extname(where) === ".gmi" && !raw) {
return send("text/html", run(handlers.gemini));
} else {
return (req, res) => {
const range = req.range(stat.size);
if(range === -2) return res.sendStatus(400);
if(range === -1) return res.sendStatus(416);
const headers = {
"Content-Type": type || "application/octet-stream",
"Content-Length": stat.size,
"Accept-Ranges": "bytes",
const type = types.get(path.extname(where).slice(1));
if(stat.size < 1024 * 1024 * 1) {
return send(type, run(handlers.file));
} else {
return (req, res) => {
const range = req.range(stat.size);
if(range === -2) return res.sendStatus(400);
if(range === -1) return res.sendStatus(416);
const headers = {
"Content-Type": type || "application/octet-stream",
"Content-Length": stat.size,
"Accept-Ranges": "bytes",
};
if(range) {
if(range.type !== "bytes") return res.sendStatus(400);
headers["Content-Range"] = `bytes ${range[0].start}-${range[0].end}/${stat.size}`;
headers["Content-Length"] = Math.abs(range[0].end - range[0].start) + 1;
log.info(`sending large file (${headers["Content-Range"]})`);
res.writeHead(206, headers);
fs.createReadStream(where, range[0]).pipe(res);
} else {
log.info("sending large file");
res.writeHead(200, headers);
fs.createReadStream(where).pipe(res);
}
};
if(range) {
if(range.type !== "bytes") return res.sendStatus(400);
headers["Content-Range"] = `bytes ${range[0].start}-${range[0].end}/${stat.size}`;
headers["Content-Length"] = Math.abs(range[0].end - range[0].start) + 1;
log.info(`sending large file (${headers["Content-Range"]})`);
res.writeHead(206, headers);
fs.createReadStream(where, range[0]).pipe(res);
} else {
log.info("sending large file");
res.writeHead(200, headers);
fs.createReadStream(where).pipe(res);
}
};
}
}
} catch {
return send("text/html", mustache.render(template404, { file, time, cache: { style: version("/assets/style.css") } }), 404);
}
function send(type, content, status = 200) {