From 3a8a2092f79d8841bffd660b86d43c1ad10cc2e6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 2 Feb 2026 22:45:19 -0700 Subject: [PATCH] Add the database sizes into the sidebar (closes #21) --- langs/en-ca.json | 7 +++++++ module/apps/ArtSidebar.mjs | 24 +++++++++++++++++++++--- module/utils/fs.mjs | 24 ++++++++++++++++++++++++ styles/apps/ArtSidebar.css | 7 +++++++ templates/ArtSidebar/artists.hbs | 11 ++++++++--- templates/ArtSidebar/tokens.hbs | 11 ++++++++--- 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/langs/en-ca.json b/langs/en-ca.json index aa6239f..e5f614a 100644 --- a/langs/en-ca.json +++ b/langs/en-ca.json @@ -1,6 +1,8 @@ { "IT": { "common": { + "artists": "Artists", + "images": "Images", "unsaved": "This change hasn't been saved, if you close without saving it will be undone." }, "apps": { @@ -22,6 +24,11 @@ }, "add-link": "Add New Link" }, + "ArtSidebar": { + "view": "View All", + "add-new": "Add New", + "db-size": "Database Size: {size}" + }, "ImageApp": { "title": { "upload": "Upload New Image", diff --git a/module/apps/ArtSidebar.mjs b/module/apps/ArtSidebar.mjs index 3e11dba..df4dd32 100644 --- a/module/apps/ArtSidebar.mjs +++ b/module/apps/ArtSidebar.mjs @@ -1,8 +1,10 @@ import { api } from "../api.mjs"; import { __ID__, filePath } from "../consts.mjs"; +import { getFileSize } from "../utils/fs.mjs"; const { HandlebarsApplicationMixin } = foundry.applications.api; const { AbstractSidebarTab } = foundry.applications.sidebar; +const { deepClone } = foundry.utils; export class ArtSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab) { // #region Options @@ -30,16 +32,32 @@ export class ArtSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab) { // #endregion Options // #region Data Prep - _preparePartContext(partID, ctx) { + async _preparePartContext(partID, ctx) { + ctx = deepClone(ctx); + switch (partID) { + case `tokens`: { + await this._prepareTokensContext(ctx); + break; + }; + case `artists`: { + await this._prepareArtistsContext(ctx); + break; + }; }; return ctx; }; - _prepareTokensContext(ctx) {}; + async _prepareTokensContext(ctx) { + const size = await getFileSize(filePath(`storage/db/images.json`)); + ctx.size = size.friendly; + }; - _prepareArtistsContext(ctx) {}; + async _prepareArtistsContext(ctx) { + const size = await getFileSize(filePath(`storage/db/artists.json`)); + ctx.size = size.friendly; + }; // #endregion Data Prep // #region Actions diff --git a/module/utils/fs.mjs b/module/utils/fs.mjs index 860da75..805a968 100644 --- a/module/utils/fs.mjs +++ b/module/utils/fs.mjs @@ -37,6 +37,30 @@ export async function lastModifiedAt(path) { }; }; +export async function getFileSize(path) { + try { + const response = await fetch(filePath(path), { method: `HEAD` }); + const bytes = response.headers.get(`Content-Length`); + if (!bytes) { return null } + + // round the non-bytes to 2 decimal places + const kilobytes = Math.floor(bytes / 10) / 100; + const megabytes = Math.floor(bytes / 10_000) / 100; + + // determine the most appropriate unit to use + let friendly = `${bytes} bytes`; + if (megabytes > 0.25) { + friendly = `${megabytes}MB`; + } else if (kilobytes > 0) { + friendly = `${kilobytes}KB`; + }; + + return { bytes, kilobytes, megabytes, friendly }; + } catch { + return null; + }; +}; + export async function getFile(path) { try { return fetchJsonWithTimeout(filePath(path)); diff --git a/styles/apps/ArtSidebar.css b/styles/apps/ArtSidebar.css index a3179cb..df26124 100644 --- a/styles/apps/ArtSidebar.css +++ b/styles/apps/ArtSidebar.css @@ -16,4 +16,11 @@ h4 { margin: 0; } + + .subtitle { + margin: 0; + text-align: center; + font-size: 0.75rem; + color: var(--color-text-subtitle); + } } diff --git a/templates/ArtSidebar/artists.hbs b/templates/ArtSidebar/artists.hbs index 96d83f7..151e301 100644 --- a/templates/ArtSidebar/artists.hbs +++ b/templates/ArtSidebar/artists.hbs @@ -1,17 +1,22 @@
-

Artists

+
+

{{ localize "IT.common.artists" }}

+

+ {{ localize "IT.apps.ArtSidebar.db-size" size=size }} +

+
diff --git a/templates/ArtSidebar/tokens.hbs b/templates/ArtSidebar/tokens.hbs index 7f7d1e8..c303f4f 100644 --- a/templates/ArtSidebar/tokens.hbs +++ b/templates/ArtSidebar/tokens.hbs @@ -1,17 +1,22 @@
-

Tokens

+
+

{{ localize "IT.common.images" }}

+

+ {{ localize "IT.apps.ArtSidebar.db-size" size=size }} +

+