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
57
files.js
57
files.js
@ -9,19 +9,16 @@ const FileType = require("file-type");
|
|||||||
|
|
||||||
const config = require("./config");
|
const config = require("./config");
|
||||||
const util = require("./util");
|
const util = require("./util");
|
||||||
const { type } = require("os");
|
|
||||||
const { ENOENT, ECONNRESET } = require("constants");
|
const { ENOENT, ECONNRESET } = require("constants");
|
||||||
|
|
||||||
const sizeUnits = [ "B", "KiB", "MiB", "GiB" ];
|
const SIZE_UNITS = [ "B", "KiB", "MiB", "GiB" ];
|
||||||
|
|
||||||
function makeCurrentPathField(path) {
|
function makeCurrentPathField(path) {
|
||||||
let current = "";
|
let current = `<a href="/">root</a>`;
|
||||||
if (path.endsWith("/"))
|
if (path.endsWith("/"))
|
||||||
path = path.slice(1, path.length-1);
|
path = path.slice(1, path.length-1);
|
||||||
const path_parts = path.split("/");
|
const path_parts = path.split("/");
|
||||||
|
|
||||||
current = `<a href="/">root</a>`;
|
|
||||||
|
|
||||||
for (i = 0; i < path_parts.length; ++i) {
|
for (i = 0; i < path_parts.length; ++i) {
|
||||||
let lnk = "";
|
let lnk = "";
|
||||||
for (j = 0; j < i+1; ++j) {
|
for (j = 0; j < i+1; ++j) {
|
||||||
@ -36,12 +33,24 @@ function makeCurrentPathField(path) {
|
|||||||
function convertSize(size) {
|
function convertSize(size) {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (; size > 1024; size /= 1024) { ++i; }
|
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) {
|
function sortByNameField(a, b) {
|
||||||
if (name.endsWith("txt") || name.endsWith("md"))
|
if (a.name > b.name)
|
||||||
return "text/plain";
|
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";
|
return "octet-stream";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,26 +80,15 @@ async function getDirectoryList(dir_path, base_url) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sort_by_name = (a, b) => {
|
dirs.sort(sortByNameField);
|
||||||
if (a.name > b.name)
|
files.sort(sortByNameField);
|
||||||
return 1;
|
|
||||||
else if (a.name < b.name)
|
|
||||||
return -1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
dirs.sort(sort_by_name);
|
return [dirs.concat(files), dirs.length, files.length,
|
||||||
files.sort(sort_by_name);
|
convertSize(total_files_size).join(' ')];
|
||||||
|
|
||||||
return [dirs.concat(files), dirs.length, files.length, convertSize(total_files_size).join(' ')];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setRoutes(router) {
|
function setRoutes(router) {
|
||||||
router.get('/(.*)?', async (ctx, next) => {
|
router.get('/(.*)?', async ctx => {
|
||||||
if (ctx.originalUrl.startsWith("/assets") || ctx.originalUrl.startsWith("/shared")) {
|
|
||||||
await next();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const file_path = path.join(config.files.file_path, decodeURI(ctx.originalUrl));
|
const file_path = path.join(config.files.file_path, decodeURI(ctx.originalUrl));
|
||||||
let stat = fs.lstatSync(file_path);
|
let stat = fs.lstatSync(file_path);
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
@ -106,17 +104,10 @@ function setRoutes(router) {
|
|||||||
total_directories: total_directories,
|
total_directories: total_directories,
|
||||||
items: items });
|
items: items });
|
||||||
} else {
|
} else {
|
||||||
let f = await FileType.fromStream(fs.createReadStream(file_path));
|
ctx.type = await fileType(file_path);
|
||||||
if (f === undefined)
|
|
||||||
ctx.type = fileType(file_path);
|
|
||||||
else
|
|
||||||
ctx.type = f.mime;
|
|
||||||
ctx.set("Content-Length", stat.size);
|
ctx.set("Content-Length", stat.size);
|
||||||
|
|
||||||
const stream = fs.createReadStream(file_path);
|
const stream = fs.createReadStream(file_path);
|
||||||
// let ch = 0;
|
|
||||||
// stream.on("end", () => { });
|
|
||||||
// stream.on("data", (chunk) => { ch += chunk.length; });
|
|
||||||
ctx.body = stream;
|
ctx.body = stream;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user