Skip to content

Commit

Permalink
Merge pull request #110 from waifuvault/landing-info
Browse files Browse the repository at this point in the history
Add Files Served and Total Size to landing page
  • Loading branch information
nakedmcse authored Mar 14, 2024
2 parents afaed7c + 110c0f0 commit 27a314d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/controllers/views/HomeView.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> {
const records = await this.fileService.getRecordCount();
const size = await this.fileService.getRecordSize();
return {
recordCount: records.toLocaleString(),
recordSize: ObjectUtils.sizeToHuman(size),
};
}

@Get("/login")
Expand Down
2 changes: 2 additions & 0 deletions src/public/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<div class="p-5 mb-4 bg-body-tertiary rounded-3 border border-primary-subtle position-relative">
<div class="container-fluid py-5 ">
<h1 class="display-5 fw-bold">No nonsense temporary file hosting</h1>
<h2 class="fw-bold">Serving <%- recordCount -%> public files totalling <%- recordSize -%>
</h2>
<p class="col-md-8 fs-4">Inspired by
<a href="https://0x0.st" target="_blank">https://0x0.st</a>, WaifuVault is a temporary file hosting service that allows for file uploads that are hosted for a set amount of time.
</p>
Expand Down
9 changes: 9 additions & 0 deletions src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ export class FileService {
return deleted;
}

public getRecordCount(): Promise<number> {
return this.repo.getRecordCount();
}

public async getRecordSize(): Promise<number> {
const entries = await this.repo.getAllEntries();
return entries.reduce((acc, currentValue) => acc + currentValue.fileSize, 0);
}

private async getFileHash(resourcePath: string): Promise<string> {
const fileBuffer = await fs.readFile(resourcePath);
const hashSum = crypto.createHash("md5");
Expand Down
16 changes: 16 additions & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 27a314d

Please sign in to comment.