Fix issue #65: Item selecting icon
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { requestFoundry } = require('../services/foundry');
|
||||
const { normalizeIconDirectory, readIconListingPage, shapeIconListing } = require('../services/foundry-icons');
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
|
||||
// The world address the icons are loaded from is kept on the campaign, so an icon
|
||||
// listing is always asked for one of the caller's own campaigns.
|
||||
async function findOwnedCampaign(campaignId, userId) {
|
||||
if (!campaignId) return null;
|
||||
return Campaign.findOne({ _id: campaignId, createdBy: userId }).select('settings').lean();
|
||||
}
|
||||
|
||||
async function proxy(path, res) {
|
||||
try {
|
||||
@@ -24,4 +34,38 @@ router.get('/actors/:id/items', (req, res) => proxy(`api/actors/${encodeURICompo
|
||||
router.get('/actors/:id', (req, res) => proxy(`api/actors/${encodeURIComponent(req.params.id)}`, res));
|
||||
router.get('/items/:id', (req, res) => proxy(`api/items/${encodeURIComponent(req.params.id)}`, res));
|
||||
|
||||
// Foundry's core icon library: the `icons/` folder of the world's data directory.
|
||||
// One folder per request, and the icons inside it paged, because that library runs
|
||||
// into the thousands and the window only ever shows where the user is standing.
|
||||
// The bridge reads it on the Foundry host; the images themselves are loaded by the
|
||||
// browser from the campaign's world address, the same way a synced item's icon is.
|
||||
router.get('/icons', async (req, res) => {
|
||||
try {
|
||||
const campaign = await findOwnedCampaign(req.query.campaign, req.user.id);
|
||||
if (!campaign) return res.status(404).json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const base = campaign.settings?.integrations?.foundryUrl || '';
|
||||
if (!base) return res.status(400).json({ status: 'error', msg: 'foundry.errors.icons-no-world-url' });
|
||||
|
||||
const dir = normalizeIconDirectory(req.query.dir);
|
||||
if (!dir) return res.status(400).json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const { limit, offset } = readIconListingPage(req.query);
|
||||
const query = new URLSearchParams({ dir, offset: String(offset), limit: String(limit) });
|
||||
const result = await requestFoundry(`api/icons?${query}`);
|
||||
if (result.status !== 200 || result.data?.status !== 'ok') {
|
||||
return res.status(502).json({ status: 'error', msg: 'foundry.errors.icons-unavailable' });
|
||||
}
|
||||
|
||||
res.json({ status: 'ok', ...shapeIconListing({ base, dir, listing: result.data }) });
|
||||
} catch (err) {
|
||||
if (err.code === 'FOUNDRY_NOT_CONFIGURED') {
|
||||
return res.status(503).json({ status: 'error', msg: 'foundry.errors.icons-unavailable' });
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
res.status(502).json({ status: 'error', msg: 'foundry.errors.icons-unavailable' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user