Merge pull request #3361 from SillyTavern/write-fallback-img

Use default avatar if imported image is corrupted
This commit is contained in:
Cohee 2025-01-26 20:36:14 +02:00 committed by GitHub
commit 6d43eea1bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -74,12 +74,18 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u
* Read the image, resize, and save it as a PNG into the buffer.
* @returns {Promise<Buffer>} 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) {
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);
}
}
const inputImage = await getInputImage();