From a21213784e74db024d1da444e453bada0ce5f7eb Mon Sep 17 00:00:00 2001 From: Walker Aldridge Date: Thu, 14 Mar 2024 11:22:24 -0500 Subject: [PATCH 1/2] Add Files Served and Total Size to landing page --- src/controllers/views/HomeView.ts | 15 ++++++++++++--- src/public/index.ejs | 2 ++ src/services/FileService.ts | 9 +++++++++ src/utils/Utils.ts | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) 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..f796c56 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) { From 110c0f0e390c1c2aa02d8d483157e7fbe246cb0a Mon Sep 17 00:00:00 2001 From: Walker Aldridge Date: Thu, 14 Mar 2024 11:43:34 -0500 Subject: [PATCH 2/2] Fix missing - on tag close --- src/public/index.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/index.ejs b/src/public/index.ejs index f796c56..e1f37fd 100644 --- a/src/public/index.ejs +++ b/src/public/index.ejs @@ -18,7 +18,7 @@

No nonsense temporary file hosting

-

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

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.