From 65e32f720d2c341a70c0826a57a67d57a6d9b77c Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:58:37 +0200 Subject: [PATCH] Use default avatar if imported image is corrupted --- src/endpoints/characters.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/endpoints/characters.js b/src/endpoints/characters.js index c7a2a02d4..cf084aca5 100644 --- a/src/endpoints/characters.js +++ b/src/endpoints/characters.js @@ -74,12 +74,17 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u * Read the image, resize, and save it as a PNG into the buffer. * @returns {Promise} Image buffer */ - function getInputImage() { - if (Buffer.isBuffer(inputFile)) { - return parseImageBuffer(inputFile, crop); - } + async function getInputImage() { + try { + if (Buffer.isBuffer(inputFile)) { + return await parseImageBuffer(inputFile, crop); + } - return tryReadImage(inputFile, crop); + return await tryReadImage(inputFile, crop); + } catch (error) { + console.log(`Failed to read image: ${inputFile}. Using a fallback image.`); + return await fs.promises.readFile(defaultAvatarPath); + } } const inputImage = await getInputImage();