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:
Alexander Andreev 2021-02-12 21:31:29 +04:00 committed by Alexander "Arav" Andreev
parent 9000d3b2f7
commit bd9964d300
Signed by: Arav
GPG Key ID: 610DF2574456329F
2 changed files with 10 additions and 2 deletions

View File

@ -49,10 +49,12 @@ function fileType(name) {
async function getDirectoryList(dir_path, base_url) {
let dirs = [];
let files = [];
let total_files_size = 0;
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);
total_files_size += stat.size;
if (stat.isDirectory())
dirs.push({
name: dirent.name,
@ -80,7 +82,7 @@ async function getDirectoryList(dir_path, base_url) {
dirs.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) {
@ -92,12 +94,17 @@ function setRoutes(router) {
const file_path = path.join(config.files.file_path, decodeURI(ctx.originalUrl));
let stat = fs.lstatSync(file_path);
if (stat.isDirectory()) {
const [items, total_directories, total_files, total_files_size]
= await getDirectoryList(file_path, ctx.originalUrl);
await ctx.render('index', {
title: "/ Files",
description: "File share.",
main_site: util.getServiceByHost(ctx.header.host),
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 {
let f = await FileType.fromStream(fs.createReadStream(file_path));
if (f === undefined)

View File

@ -8,6 +8,7 @@ block nav
block content
section#files
span#current-path!= current_path
p Files: #{total_files} (#{total_files_size}); Directories: #{total_directories}.
table
thead
tr