From 659906e562e796b7a5f31b6a22b7738f4dfd2861 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 23 Apr 2025 10:40:06 +0100 Subject: [PATCH] Add catch to image grid generator --- src/helpers/ImageHelper.ts | 71 ++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/helpers/ImageHelper.ts b/src/helpers/ImageHelper.ts index 79c1c6b..163c8ec 100644 --- a/src/helpers/ImageHelper.ts +++ b/src/helpers/ImageHelper.ts @@ -26,46 +26,51 @@ export default class ImageHelper { const ctx = canvas.getContext("2d"); for (let i = 0; i < cards.length; i++) { - const card = cards[i]; + try { + const card = cards[i]; - const filePath = path.join(process.env.DATA_DIR!, "cards", card.path); + const filePath = path.join(process.env.DATA_DIR!, "cards", card.path); - let bitmap: Bitmap; + let bitmap: Bitmap; - if (existsSync(filePath)) { - const data = await Jimp.read(filePath); + if (existsSync(filePath)) { + const data = await Jimp.read(filePath); - bitmap = data.bitmap; - } else if (card.path.startsWith("http://") || card.path.startsWith("https://")) { - const response = await axios.get(card.path, { responseType: "arraybuffer" }); - const buffer = Buffer.from(response.data); - const data = await Jimp.fromBuffer(buffer); + bitmap = data.bitmap; + } else if (card.path.startsWith("http://") || card.path.startsWith("https://")) { + const response = await axios.get(card.path, { responseType: "arraybuffer" }); + const buffer = Buffer.from(response.data); + const data = await Jimp.fromBuffer(buffer); - bitmap = data.bitmap; - } else { - AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`); - continue; - } - - const imageData = Jimp.fromBitmap(bitmap); - - if (userId != null) { - const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id); - - if (!claimed || claimed.Quantity == 0) { - imageData.greyscale(); + bitmap = data.bitmap; + } else { + AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`); + continue; } + + const imageData = Jimp.fromBitmap(bitmap); + + if (userId != null) { + const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id); + + if (!claimed || claimed.Quantity == 0) { + imageData.greyscale(); + } + } + + const image = await loadImage(await imageData.getBuffer("image/png")); + + const x = i % gridWidth; + const y = Math.floor(i / gridWidth); + + const imageX = imageWidth * x; + const imageY = imageHeight * y; + + ctx.drawImage(image, imageX, imageY); + } + catch (e) { + AppLogger.CatchError("ImageHelper", e); } - - const image = await loadImage(await imageData.getBuffer("image/png")); - - const x = i % gridWidth; - const y = Math.floor(i / gridWidth); - - const imageX = imageWidth * x; - const imageY = imageHeight * y; - - ctx.drawImage(image, imageX, imageY); } return canvas.toBuffer();