Add the database sizes into the sidebar (closes #21)
This commit is contained in:
parent
1e68187959
commit
3a8a2092f7
6 changed files with 75 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue