Moved all code for file type negotiation to fileType function. Removed handling of static routes in main route. Moved comparing function out of getDirectoryList as a standalone function. And renamed sizeUnits to SIZE_UNITS, it's a global constant after all. :)
This commit is contained in:
parent
682cb65feb
commit
714b0821fb
55
files.js
55
files.js
@ -9,19 +9,16 @@ const FileType = require("file-type");
|
||||
|
||||
const config = require("./config");
|
||||
const util = require("./util");
|
||||
const { type } = require("os");
|
||||
const { ENOENT, ECONNRESET } = require("constants");
|
||||
|
||||
const sizeUnits = [ "B", "KiB", "MiB", "GiB" ];
|
||||
const SIZE_UNITS = [ "B", "KiB", "MiB", "GiB" ];
|
||||
|
||||
function makeCurrentPathField(path) {
|
||||
let current = "";
|
||||
let current = `<a href="/">root</a>`;
|
||||
if (path.endsWith("/"))
|
||||
path = path.slice(1, path.length-1);
|
||||
const path_parts = path.split("/");
|
||||
|
||||
current = `<a href="/">root</a>`;
|
||||
|
||||
for (i = 0; i < path_parts.length; ++i) {
|
||||
let lnk = "";
|
||||
for (j = 0; j < i+1; ++j) {
|
||||
@ -36,12 +33,24 @@ function makeCurrentPathField(path) {
|
||||
function convertSize(size) {
|
||||
let i = 0;
|
||||
for (; size > 1024; size /= 1024) { ++i; }
|
||||
return [i > 0 ? size.toFixed(2) : size, sizeUnits[i]];
|
||||
return [i > 0 ? size.toFixed(2) : size, SIZE_UNITS[i]];
|
||||
}
|
||||
|
||||
function fileType(name) {
|
||||
if (name.endsWith("txt") || name.endsWith("md"))
|
||||
function sortByNameField(a, b) {
|
||||
if (a.name > b.name)
|
||||
return 1;
|
||||
else if (a.name < b.name)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function fileType(name) {
|
||||
let f = await FileType.fromStream(fs.createReadStream(name));
|
||||
if (f === undefined)
|
||||
if (name.endsWith("txt"))
|
||||
return "text/plain";
|
||||
else
|
||||
return f.mime;
|
||||
return "octet-stream";
|
||||
}
|
||||
|
||||
@ -71,26 +80,15 @@ async function getDirectoryList(dir_path, base_url) {
|
||||
}
|
||||
}
|
||||
|
||||
const sort_by_name = (a, b) => {
|
||||
if (a.name > b.name)
|
||||
return 1;
|
||||
else if (a.name < b.name)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
dirs.sort(sortByNameField);
|
||||
files.sort(sortByNameField);
|
||||
|
||||
dirs.sort(sort_by_name);
|
||||
files.sort(sort_by_name);
|
||||
|
||||
return [dirs.concat(files), dirs.length, files.length, convertSize(total_files_size).join(' ')];
|
||||
return [dirs.concat(files), dirs.length, files.length,
|
||||
convertSize(total_files_size).join(' ')];
|
||||
}
|
||||
|
||||
function setRoutes(router) {
|
||||
router.get('/(.*)?', async (ctx, next) => {
|
||||
if (ctx.originalUrl.startsWith("/assets") || ctx.originalUrl.startsWith("/shared")) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
router.get('/(.*)?', async ctx => {
|
||||
const file_path = path.join(config.files.file_path, decodeURI(ctx.originalUrl));
|
||||
let stat = fs.lstatSync(file_path);
|
||||
if (stat.isDirectory()) {
|
||||
@ -106,17 +104,10 @@ function setRoutes(router) {
|
||||
total_directories: total_directories,
|
||||
items: items });
|
||||
} else {
|
||||
let f = await FileType.fromStream(fs.createReadStream(file_path));
|
||||
if (f === undefined)
|
||||
ctx.type = fileType(file_path);
|
||||
else
|
||||
ctx.type = f.mime;
|
||||
ctx.type = await fileType(file_path);
|
||||
ctx.set("Content-Length", stat.size);
|
||||
|
||||
const stream = fs.createReadStream(file_path);
|
||||
// let ch = 0;
|
||||
// stream.on("end", () => { });
|
||||
// stream.on("data", (chunk) => { ch += chunk.length; });
|
||||
ctx.body = stream;
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user