Added showing stats on how many files and directories in a current directory, and a total size of files in it.
This commit is contained in:
parent
9000d3b2f7
commit
bd9964d300
11
files.js
11
files.js
@ -49,10 +49,12 @@ function fileType(name) {
|
|||||||
async function getDirectoryList(dir_path, base_url) {
|
async function getDirectoryList(dir_path, base_url) {
|
||||||
let dirs = [];
|
let dirs = [];
|
||||||
let files = [];
|
let files = [];
|
||||||
|
let total_files_size = 0;
|
||||||
let d = await fs.promises.opendir(dir_path);
|
let d = await fs.promises.opendir(dir_path);
|
||||||
for await (const dirent of d) {
|
for await (const dirent of d) {
|
||||||
const stat = fs.lstatSync(path.join(dir_path, dirent.name));
|
const stat = fs.lstatSync(path.join(dir_path, dirent.name));
|
||||||
const [s, u] = convertSize(stat.size);
|
const [s, u] = convertSize(stat.size);
|
||||||
|
total_files_size += stat.size;
|
||||||
if (stat.isDirectory())
|
if (stat.isDirectory())
|
||||||
dirs.push({
|
dirs.push({
|
||||||
name: dirent.name,
|
name: dirent.name,
|
||||||
@ -80,7 +82,7 @@ async function getDirectoryList(dir_path, base_url) {
|
|||||||
dirs.sort(sort_by_name);
|
dirs.sort(sort_by_name);
|
||||||
files.sort(sort_by_name);
|
files.sort(sort_by_name);
|
||||||
|
|
||||||
return dirs.concat(files);
|
return [dirs.concat(files), dirs.length, files.length, convertSize(total_files_size)];
|
||||||
}
|
}
|
||||||
|
|
||||||
function setRoutes(router) {
|
function setRoutes(router) {
|
||||||
@ -92,12 +94,17 @@ function setRoutes(router) {
|
|||||||
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()) {
|
||||||
|
const [items, total_directories, total_files, total_files_size]
|
||||||
|
= await getDirectoryList(file_path, ctx.originalUrl);
|
||||||
await ctx.render('index', {
|
await ctx.render('index', {
|
||||||
title: "/ Files",
|
title: "/ Files",
|
||||||
description: "File share.",
|
description: "File share.",
|
||||||
main_site: util.getServiceByHost(ctx.header.host),
|
main_site: util.getServiceByHost(ctx.header.host),
|
||||||
current_path: makeCurrentPathField(decodeURI(ctx.originalUrl)),
|
current_path: makeCurrentPathField(decodeURI(ctx.originalUrl)),
|
||||||
items: await getDirectoryList(file_path, ctx.originalUrl) });
|
total_files: total_files,
|
||||||
|
total_files_size: total_files_size,
|
||||||
|
total_directories: total_directories,
|
||||||
|
items: items });
|
||||||
} else {
|
} else {
|
||||||
let f = await FileType.fromStream(fs.createReadStream(file_path));
|
let f = await FileType.fromStream(fs.createReadStream(file_path));
|
||||||
if (f === undefined)
|
if (f === undefined)
|
||||||
|
@ -8,6 +8,7 @@ block nav
|
|||||||
block content
|
block content
|
||||||
section#files
|
section#files
|
||||||
span#current-path!= current_path
|
span#current-path!= current_path
|
||||||
|
p Files: #{total_files} (#{total_files_size}); Directories: #{total_directories}.
|
||||||
table
|
table
|
||||||
thead
|
thead
|
||||||
tr
|
tr
|
||||||
|
Loading…
Reference in New Issue
Block a user