diff --git a/src/controllers/views/HomeView.ts b/src/controllers/views/HomeView.ts index 203d330..327b46d 100644 --- a/src/controllers/views/HomeView.ts +++ b/src/controllers/views/HomeView.ts @@ -1,14 +1,23 @@ import { Get, Hidden, View } from "@tsed/schema"; -import { Controller } from "@tsed/di"; +import { Controller, Inject } from "@tsed/di"; import { Req, Res } from "@tsed/common"; +import { FileService } from "../../services/FileService.js"; +import { ObjectUtils } from "../../utils/Utils.js"; @Controller("/") @Hidden() export class HomeView { + public constructor(@Inject() private fileService: FileService) {} + @Get() @View("index.ejs") - public showRoot(): unknown { - return null; + public async showRoot(): Promise { + const records = await this.fileService.getRecordCount(); + const size = await this.fileService.getRecordSize(); + return { + recordCount: records.toLocaleString(), + recordSize: ObjectUtils.sizeToHuman(size), + }; } @Get("/login") diff --git a/src/public/index.ejs b/src/public/index.ejs index b5eff0c..e1f37fd 100644 --- a/src/public/index.ejs +++ b/src/public/index.ejs @@ -18,6 +18,8 @@

No nonsense temporary file hosting

+

Serving <%- recordCount -%> public files totalling <%- recordSize -%> +

Inspired by https://0x0.st, WaifuVault is a temporary file hosting service that allows for file uploads that are hosted for a set amount of time.

diff --git a/src/services/FileService.ts b/src/services/FileService.ts index c7265eb..d399760 100644 --- a/src/services/FileService.ts +++ b/src/services/FileService.ts @@ -228,6 +228,15 @@ export class FileService { return deleted; } + public getRecordCount(): Promise { + return this.repo.getRecordCount(); + } + + public async getRecordSize(): Promise { + const entries = await this.repo.getAllEntries(); + return entries.reduce((acc, currentValue) => acc + currentValue.fileSize, 0); + } + private async getFileHash(resourcePath: string): Promise { const fileBuffer = await fs.readFile(resourcePath); const hashSum = crypto.createHash("md5"); diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index d027577..76b23d2 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -13,6 +13,22 @@ export class ObjectUtils { return matches && matches[0] ? parseInt(matches[0]) : 0; } + public static sizeToHuman(value: number): string { + const sizeKB = Math.floor(value / 1024); + const sizeMB = Math.floor(sizeKB / 1024); + const sizeGB = Math.floor(sizeMB / 1024); + if (value < 1024) { + return `${value} B`; + } + if (sizeKB < 1024) { + return `${sizeKB} KB`; + } + if (sizeMB < 1024) { + return `${sizeMB} MB`; + } + return `${sizeGB} GB`; + } + public static timeToHuman(value: number, timeUnit: TIME_UNIT = TIME_UNIT.milliseconds): string { let seconds: number; if (timeUnit === TIME_UNIT.milliseconds) {