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 1/2] 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(); From 999da4945aaf1da6f6d4ff1e9e314c11f0ccfeb1 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 26 Jan 2025 20:29:04 +0200 Subject: [PATCH 2/2] Fix error log --- src/endpoints/characters.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/endpoints/characters.js b/src/endpoints/characters.js index cf084aca5..1e5df80bd 100644 --- a/src/endpoints/characters.js +++ b/src/endpoints/characters.js @@ -82,7 +82,8 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u return await tryReadImage(inputFile, crop); } catch (error) { - console.log(`Failed to read image: ${inputFile}. Using a fallback image.`); + const message = Buffer.isBuffer(inputFile) ? 'Failed to read image buffer.' : `Failed to read image: ${inputFile}.`; + console.warn(message, 'Using a fallback image.', error); return await fs.promises.readFile(defaultAvatarPath); } }