2021-02-10 01:01:50 +04:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const Koa = require("koa");
|
|
|
|
const koaPug = require("koa-pug");
|
|
|
|
const koaRouter = require("koa-router");
|
|
|
|
const koaRange = require("koa-range");
|
|
|
|
|
|
|
|
const config = require("./config");
|
|
|
|
const util = require("./util");
|
|
|
|
const { ENOENT, ECONNRESET } = require("constants");
|
|
|
|
|
2021-02-16 02:16:03 +04:00
|
|
|
const SIZE_UNITS = [ "B", "KiB", "MiB", "GiB" ];
|
2021-02-10 01:01:50 +04:00
|
|
|
|
|
|
|
function makeCurrentPathField(path) {
|
2021-02-16 02:16:03 +04:00
|
|
|
let current = `<a href="/">root</a>`;
|
2021-02-10 01:01:50 +04:00
|
|
|
if (path.endsWith("/"))
|
|
|
|
path = path.slice(1, path.length-1);
|
|
|
|
const path_parts = path.split("/");
|
|
|
|
|
|
|
|
for (i = 0; i < path_parts.length; ++i) {
|
|
|
|
let lnk = "";
|
|
|
|
for (j = 0; j < i+1; ++j) {
|
|
|
|
lnk += `/${path_parts[j]}`;
|
|
|
|
}
|
|
|
|
current += `/<a href="${encodeURI(lnk)}/">${path_parts[i]}</a>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertSize(size) {
|
|
|
|
let i = 0;
|
|
|
|
for (; size > 1024; size /= 1024) { ++i; }
|
2021-02-16 02:16:03 +04:00
|
|
|
return [i > 0 ? size.toFixed(2) : size, SIZE_UNITS[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortByNameField(a, b) {
|
|
|
|
if (a.name > b.name)
|
|
|
|
return 1;
|
|
|
|
else if (a.name < b.name)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2021-02-10 01:01:50 +04:00
|
|
|
}
|
|
|
|
|
2021-02-17 03:05:50 +04:00
|
|
|
async function getDirectoryList(dir_path, orig_url) {
|
2021-02-10 01:01:50 +04:00
|
|
|
let dirs = [];
|
|
|
|
let files = [];
|
2021-02-12 21:31:29 +04:00
|
|
|
let total_files_size = 0;
|
2021-02-10 01:01:50 +04:00
|
|
|
let d = await fs.promises.opendir(dir_path);
|
|
|
|
for await (const dirent of d) {
|
|
|
|
const stat = fs.lstatSync(path.join(dir_path, dirent.name));
|
|
|
|
const [s, u] = convertSize(stat.size);
|
|
|
|
if (stat.isDirectory())
|
|
|
|
dirs.push({
|
|
|
|
name: dirent.name,
|
2021-02-17 02:05:54 +04:00
|
|
|
link: `${encodeURIComponent(dirent.name)}/`,
|
2021-02-10 01:56:53 +04:00
|
|
|
datetime: util.datetime(stat.mtime, util.formats.file_date),
|
2021-02-10 01:01:50 +04:00
|
|
|
size: "DIR",
|
|
|
|
size_unit: '' });
|
2021-02-12 22:51:48 +04:00
|
|
|
else {
|
|
|
|
total_files_size += stat.size;
|
2021-02-10 01:01:50 +04:00
|
|
|
files.push({
|
|
|
|
name: dirent.name,
|
2021-02-17 03:05:50 +04:00
|
|
|
link: `/files${orig_url}${encodeURIComponent(dirent.name)}`,
|
2021-02-10 01:56:53 +04:00
|
|
|
datetime: util.datetime(stat.mtime, util.formats.file_date),
|
2021-02-10 01:01:50 +04:00
|
|
|
size: +s,
|
|
|
|
size_unit: u });
|
2021-02-12 22:51:48 +04:00
|
|
|
}
|
2021-02-10 01:01:50 +04:00
|
|
|
}
|
|
|
|
|
2021-02-16 02:16:03 +04:00
|
|
|
dirs.sort(sortByNameField);
|
|
|
|
files.sort(sortByNameField);
|
2021-02-10 01:01:50 +04:00
|
|
|
|
2021-02-16 02:16:03 +04:00
|
|
|
return [dirs.concat(files), dirs.length, files.length,
|
|
|
|
convertSize(total_files_size).join(' ')];
|
2021-02-10 01:01:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function setRoutes(router) {
|
2021-02-16 02:16:03 +04:00
|
|
|
router.get('/(.*)?', async ctx => {
|
2021-02-10 01:01:50 +04:00
|
|
|
const file_path = path.join(config.files.file_path, decodeURI(ctx.originalUrl));
|
|
|
|
let stat = fs.lstatSync(file_path);
|
|
|
|
if (stat.isDirectory()) {
|
2021-02-12 21:31:29 +04:00
|
|
|
const [items, total_directories, total_files, total_files_size]
|
2021-02-17 03:05:50 +04:00
|
|
|
= await getDirectoryList(file_path, decodeURI(ctx.originalUrl));
|
2021-02-10 01:01:50 +04:00
|
|
|
await ctx.render('index', {
|
|
|
|
title: "/ Files",
|
|
|
|
description: "File share.",
|
2021-02-11 02:57:18 +04:00
|
|
|
main_site: util.getServiceByHost(ctx.header.host),
|
2021-02-10 01:01:50 +04:00
|
|
|
current_path: makeCurrentPathField(decodeURI(ctx.originalUrl)),
|
2021-02-12 21:31:29 +04:00
|
|
|
total_files: total_files,
|
|
|
|
total_files_size: total_files_size,
|
|
|
|
total_directories: total_directories,
|
|
|
|
items: items });
|
2021-02-10 01:01:50 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = () => {
|
|
|
|
const app = new Koa();
|
|
|
|
const pug = new koaPug({
|
|
|
|
viewPath: path.join(__dirname, "views", "files"),
|
|
|
|
locals: {
|
|
|
|
moment: date => util.datetime(date, util.formats.file_date) },
|
|
|
|
app: app
|
|
|
|
});
|
|
|
|
|
|
|
|
const radio_router = koaRouter();
|
|
|
|
setRoutes(radio_router);
|
|
|
|
|
2021-02-10 03:24:14 +04:00
|
|
|
app.proxy = true;
|
2021-02-10 01:01:50 +04:00
|
|
|
app
|
|
|
|
.use(koaRange)
|
|
|
|
.use(radio_router.routes())
|
|
|
|
.on("error", err => { if (!err.code == ECONNRESET || !err.code == ENOENT) console.log(err.code); })
|
|
|
|
.listen(config.files.port, config.files.host);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|