This commit is contained in:
110
backend/src/services/foundry-icons.js
Normal file
110
backend/src/services/foundry-icons.js
Normal file
@@ -0,0 +1,110 @@
|
||||
// Foundry's core icon library is the `icons/` folder of its data directory: a few
|
||||
// thousand images spread over its own folder tree. Dragonroll never reads that
|
||||
// tree itself — the bridge on the Foundry host lists one folder per request (see
|
||||
// dragonroll-foundry/bridge/server.js) — but everything that goes over the wire is
|
||||
// normalised here first, so a caller cannot ask for a folder outside the icon root.
|
||||
|
||||
const ICON_ROOT = 'icons';
|
||||
|
||||
// Image formats Foundry's own file picker offers.
|
||||
const ICON_EXTENSIONS = ['.svg', '.png', '.webp', '.jpg', '.jpeg', '.gif', '.avif'];
|
||||
|
||||
// How many icons one request lists. The window asks again for the next page, so a
|
||||
// folder with hundreds of them is never sent or rendered all at once.
|
||||
const DEFAULT_ICON_PAGE_SIZE = 120;
|
||||
const MAX_ICON_PAGE_SIZE = 500;
|
||||
|
||||
// The folder a listing request asks for, inside the icon root: `icons`, `icons/svg`,
|
||||
// ... Empty asks for the root. Anything that tries to leave the root is invalid.
|
||||
function normalizeIconDirectory(input) {
|
||||
const raw = String(input ?? '').trim().replace(/\\/g, '/').replace(/^\/+|\/+$/g, '');
|
||||
if (!raw || raw === '.') return ICON_ROOT;
|
||||
|
||||
const segments = raw.split('/').filter((segment) => segment && segment !== '.');
|
||||
if (!segments.length || segments.includes('..')) return null;
|
||||
if (segments[0] !== ICON_ROOT) return null;
|
||||
return segments.join('/');
|
||||
}
|
||||
|
||||
// The folder above an icon folder, or null at the root.
|
||||
function parentIconDirectory(dir) {
|
||||
const normalized = normalizeIconDirectory(dir);
|
||||
if (!normalized || normalized === ICON_ROOT) return null;
|
||||
return normalized.slice(0, normalized.lastIndexOf('/'));
|
||||
}
|
||||
function iconName(filePath) {
|
||||
const path = String(filePath ?? '');
|
||||
return path.slice(path.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
function isIconFile(filePath) {
|
||||
const lower = String(filePath ?? '').toLowerCase();
|
||||
return ICON_EXTENSIONS.some((extension) => lower.endsWith(extension));
|
||||
}
|
||||
|
||||
// Where the browser loads a Foundry data file from: Foundry serves its data
|
||||
// directory over the world's own address, which is the one the campaign keeps
|
||||
// (settings.integrations.foundryUrl). Items store icons as these absolute URLs,
|
||||
// the same way a synced Foundry item's icon arrives as one.
|
||||
function joinFoundryAssetUrl(baseUrl, filePath) {
|
||||
const base = String(baseUrl ?? '').trim();
|
||||
const file = String(filePath ?? '').trim().replace(/^\/+/, '');
|
||||
if (!base || !file) return '';
|
||||
return `${base.replace(/\/+$/, '')}/${encodeURI(file)}`;
|
||||
}
|
||||
|
||||
// The page a request asks for. Directories are always listed whole — the window
|
||||
// needs every subfolder to navigate — so paging only ever covers the icons.
|
||||
function readIconListingPage(query) {
|
||||
const requestedLimit = Number(query?.limit);
|
||||
const limit = Number.isFinite(requestedLimit)
|
||||
? Math.min(MAX_ICON_PAGE_SIZE, Math.max(1, Math.trunc(requestedLimit)))
|
||||
: DEFAULT_ICON_PAGE_SIZE;
|
||||
const requestedOffset = Number(query?.offset);
|
||||
const offset = Number.isFinite(requestedOffset) ? Math.max(0, Math.trunc(requestedOffset)) : 0;
|
||||
|
||||
return { limit, offset };
|
||||
}
|
||||
|
||||
// The bridge answers with plain relative paths; this is the shape the icon window
|
||||
// works with: the folder it shows, the one above it, its subfolders, and its icons
|
||||
// with a url to load. `next` is the offset of the next page, null once it is all
|
||||
// listed, which is what keeps a big library lazy to load.
|
||||
function shapeIconListing({ base = '', dir, listing = {} } = {}) {
|
||||
const current = normalizeIconDirectory(dir ?? listing.dir) || ICON_ROOT;
|
||||
|
||||
const folders = [];
|
||||
const seenFolders = new Set();
|
||||
for (const entry of Array.isArray(listing.directories) ? listing.directories : []) {
|
||||
const path = normalizeIconDirectory(typeof entry === 'string' ? entry : entry?.path);
|
||||
if (!path || path === current || seenFolders.has(path)) continue;
|
||||
seenFolders.add(path);
|
||||
folders.push({ path, name: iconName(path) });
|
||||
}
|
||||
|
||||
const files = (Array.isArray(listing.files) ? listing.files : [])
|
||||
.map((entry) => String(typeof entry === 'string' ? entry : entry?.path ?? '').trim().replace(/^\/+/, ''))
|
||||
.filter((entry) => entry.startsWith(`${ICON_ROOT}/`) && isIconFile(entry))
|
||||
.map((entry) => ({ path: entry, name: iconName(entry), url: joinFoundryAssetUrl(base, entry) }));
|
||||
|
||||
const offset = Number.isFinite(Number(listing.offset)) ? Math.max(0, Math.trunc(Number(listing.offset))) : 0;
|
||||
const next = Number.isFinite(Number(listing.next)) && Number(listing.next) > offset
|
||||
? Math.trunc(Number(listing.next))
|
||||
: null;
|
||||
|
||||
return { dir: current, parent: parentIconDirectory(current), folders, files, offset, next };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ICON_ROOT,
|
||||
ICON_EXTENSIONS,
|
||||
DEFAULT_ICON_PAGE_SIZE,
|
||||
MAX_ICON_PAGE_SIZE,
|
||||
normalizeIconDirectory,
|
||||
parentIconDirectory,
|
||||
iconName,
|
||||
isIconFile,
|
||||
joinFoundryAssetUrl,
|
||||
readIconListingPage,
|
||||
shapeIconListing
|
||||
};
|
||||
Reference in New Issue
Block a user