Fix issue #35: Dragonroll frontend: item modifications
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-14 18:08:19 +00:00
parent 2c78ad039a
commit 22c6403736
15 changed files with 404 additions and 20 deletions

View File

@@ -17,6 +17,12 @@ const ItemSchema = new Schema({
weight: { type: Number, min: 0 },
properties: { type: String, default: '' },
description: { type: String, default: '' },
// Item icon: an uploaded filename served from /api/public, or a full URL
// (Foundry sends its own icon as one).
image: { type: String, default: '' },
// The icon Foundry last sent for this item, kept even after the user picks
// their own so Dragonroll can still offer Foundry's back.
foundryImage: { type: String, default: '' },
campaign: {
type: Schema.Types.ObjectId,
ref: 'Campaign',

View File

@@ -109,6 +109,9 @@ async function saveFoundryItem(campaign, source, fullSync = false) {
item.foundryItemId = foundryItemId;
if (!fullSync || item.isNew) Object.assign(item, fields);
// Foundry's own icon is kept on every save, not only when the edits above
// land: the item window offers it back after the user picked their own.
if (fields.image) item.foundryImage = fields.image;
if (!item.foundryNames.includes(fields.name)) {
item.foundryNames = [...item.foundryNames, fields.name].slice(-MAX_FOUNDRY_NAMES);
}

View File

@@ -1,6 +1,8 @@
const express = require('express');
const router = express.Router();
const upload = require('../services/storage');
const Campaign = require('../models/Campaign');
const Item = require('../models/Item');
@@ -44,7 +46,7 @@ router.post('/create', async (req, res) => {
router.post('/update', async (req, res) => {
try {
const { id, name, category, rarity, quantity, weight, properties, description } = req.body;
const { id, name, category, rarity, quantity, weight, properties, description, image } = req.body;
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
const item = await Item.findById(id);
@@ -58,6 +60,7 @@ router.post('/update', async (req, res) => {
if (weight !== undefined && weight !== '') item.weight = Number(weight);
if (properties !== undefined) item.properties = properties;
if (description !== undefined) item.description = description;
if (image !== undefined) item.image = String(image);
await item.save();
res.json({ status: 'ok', item });
@@ -67,6 +70,25 @@ router.post('/update', async (req, res) => {
}
});
// Upload item image post
router.post("/upload-image", upload.single("image"), async (req, res) => {
try {
const itemId = req.query.item || req.body.item;
if (!itemId || !req.file) return res.json({ status: 'error', msg: 'errors.missing-data' });
const item = await Item.findById(itemId);
if (!item) return res.json({ status: 'error', msg: 'errors.notfound' });
if (!(await userOwnsCampaign(item.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
item.image = req.file.filename;
await item.save();
res.json({ status: "ok", msg: "uploaded", item });
} catch (err) {
console.error(err);
res.json({ status: "error", msg: "internal" });
}
});
router.post('/delete', async (req, res) => {
try {
const { id } = req.body;

View File

@@ -101,7 +101,9 @@ function foundryItemToItemData(foundryItem) {
quantity: Math.max(1, Math.trunc(numberOr(system.quantity, 1))),
weight: Math.max(0, numberOr(system.weight?.value ?? system.weight, 0)),
properties: foundryItemProperties(foundryItem),
description: plainText(system.description?.value ?? system.description)
description: plainText(system.description?.value ?? system.description),
// The module sends Foundry's icon as an absolute URL, like actors do.
image: asText(foundryItem.img || foundryItem.image)
};
}