Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning if files on filesystem does not match database #97

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/model/rest/Stats.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { Property } from "@tsed/schema";
import { Builder } from "builder-pattern";
import { FileEntry } from "./FileEntry.js";
import { FileUtils } from "../../utils/Utils.js";

export class Stats {
@Property()
public totalFileCount: number;

@Property()
public realFileCount: number;

@Property()
public totalFileSize: number;

@Property()
public entries: FileEntry[];

public static buildStats(entries: FileEntry[]): Stats {
public static async buildStats(entries: FileEntry[]): Promise<Stats> {
const realFiles = await FileUtils.getFilesCount();
const fileSizes = entries.reduce((acc, currentValue) => acc + currentValue.fileSize, 0);
const statsBuilder = Builder(Stats).totalFileCount(entries.length).totalFileSize(fileSizes).entries(entries);
const statsBuilder = Builder(Stats)
.totalFileCount(entries.length)
.realFileCount(realFiles)
.totalFileSize(fileSizes)
.entries(entries);
return statsBuilder.build();
}
}
7 changes: 6 additions & 1 deletion src/public/secure/stats.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@

am5.ready(function () {
getStats().then(stats => {
document.getElementById('totalFiles').innerText = stats.totalFileCount;
if(stats.totalFileCount === stats.realFileCount) {
document.getElementById('totalFiles').innerText = stats.totalFileCount;
} else {
document.getElementById('totalFiles').innerText = `${stats.totalFileCount} (${stats.realFileCount} on filesystem)`;
document.getElementById('totalFiles').className = "text-danger";
}
document.getElementById('totalSize').innerText = sizeReadable(stats.totalFileSize);

//File Protection
Expand Down
5 changes: 3 additions & 2 deletions src/services/AdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export class AdminService {
@Constant(GlobalEnv.BASE_URL)
private readonly baseUrl: string;

public getStatsData(entries: FileEntry[]): Stats {
return Stats.buildStats(entries);
public async getStatsData(entries: FileEntry[]): Promise<Stats> {
const stats = await Stats.buildStats(entries);
VictoriqueMoe marked this conversation as resolved.
Show resolved Hide resolved
return stats;
}

public async getAllEntries(): Promise<FileEntry[]> {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { FileUploadModel } from "../model/db/FileUpload.model.js";
import TIME_UNIT from "../model/constants/TIME_UNIT.js";
import process from "node:process";
import type { Request } from "express";
import fs from "node:fs/promises";

export class ObjectUtils {
public static getNumber(source: string): number {
Expand Down Expand Up @@ -103,6 +104,25 @@ export class FileUtils {
public static getExpiresBySize(filesize: number): number {
return Date.now() + this.getTimeLeftBySize(filesize);
}

public static async getFilesCount(): Promise<number> {
try {
const realFiles = await fs.readdir(filesDir, { withFileTypes: true });
const fileCount = (
await Promise.all(
realFiles.map(item => {
if (item.isDirectory()) {
return 0;
}
return 1;
}),
)
).reduce((acc, cur) => acc + cur, 0);
return fileCount;
} catch {
return 0;
}
}
nakedmcse marked this conversation as resolved.
Show resolved Hide resolved
}

export class NetworkUtils {
Expand Down