From d35e389882e94124a668f7825fb9e27b4cf04d48 Mon Sep 17 00:00:00 2001 From: SG-O Date: Sun, 10 Jul 2022 01:31:37 +0200 Subject: [PATCH] Added several config options This includes the ability to enable or disable the resizing of oversized images. The ability to allow the upload of animated stickers. The possibillity to disallow the upload of stickers that are not 512x512 when resizing is enabled --- config/default.yaml | 9 ++++ src/config.ts | 3 ++ src/matrix/MatrixStickerBot.ts | 85 ++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 7a4287a..bd55489 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -79,6 +79,15 @@ stickers: # Whether or not to allow people to add custom sticker packs enabled: true + # Whether to resize images that are not 512x512 during import. This might slow down the import of animated Stickers. + resize: true + + # Whether to allow animated stickers. If set to false animations will be stripped from images. + allowAnimated: true + + # Whether to skip over images that are not 512x512. Ignored when resize is true. + verifyImageSize: true + # The sticker manager bot to promote stickerBot: "@stickers:t2bot.io" diff --git a/src/config.ts b/src/config.ts index fad170c..39bc6d9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -36,6 +36,9 @@ export interface DimensionConfig { }; stickers: { enabled: boolean; + resize: boolean; + allowAnimated: boolean; + verifyImageSize: boolean; stickerBot: string; managerUrl: string; }; diff --git a/src/matrix/MatrixStickerBot.ts b/src/matrix/MatrixStickerBot.ts index ca38fec..0b679a5 100644 --- a/src/matrix/MatrixStickerBot.ts +++ b/src/matrix/MatrixStickerBot.ts @@ -121,19 +121,31 @@ class _MatrixStickerBot { var mime = mx.parseFileHeaderMIME(downImage); if (!mime) continue; - const origImage = await sharp(downImage, {animated: true}); - const metadata = await origImage.metadata(); - var size = metadata.height; - if (metadata.width > metadata.height) { - metadata.width; + const origImage = await sharp(downImage, {animated: config.stickers.allowAnimated}); + var resizedImage:any; + if (config.stickers.resize) { + const metadata = await origImage.metadata(); + var size = metadata.height; + if (metadata.width > metadata.height) { + metadata.width; + } + if (size > 512) size = 512; + resizedImage = await origImage.resize({ + width: size, + height: size, + fit: 'contain', + background: 'rgba(0,0,0,0)', + }); + } else { + if (config.stickers.verifyImageSize) { + const metadata = await origImage.metadata(); + if (metadata.width !== metadata.height || metadata.width !== 512) { + LogService.info("MatrixStickerBot", `Sticker ${stickerId} has an invalid size. Skipping...`); + continue; + } + } + resizedImage = origImage; } - if (size > 512) size = 512; - const resizedImage = await origImage.resize({ - width: size, - height: size, - fit: 'contain', - background: 'rgba(0,0,0,0)', - }); var imageUpload; var thumbUpload; if (mime === "image/png") { @@ -141,14 +153,28 @@ class _MatrixStickerBot { thumbUpload = imageUpload; } if (mime === "image/gif" || mime === "image/webp" || mime === "image/avif-sequence") { - imageUpload = await resizedImage.webp({quality: 60, effort: 6}).toBuffer(); - thumbUpload = await sharp(downImage, {animated: false}).resize({ - width: size, - height: size, - fit: 'contain', - background: 'rgba(0,0,0,0)', - }).webp({quality: 50}).toBuffer(); - mime = "image/webp"; + if (config.stickers.allowAnimated) { + if (config.stickers.resize){ + imageUpload = await resizedImage.webp({quality: 60, effort: 6}).toBuffer(); + mime = "image/webp"; + thumbUpload = await sharp(downImage, {animated: false}).webp({quality: 50}).toBuffer(); + } else { + imageUpload = null; + if (mime === "image/gif") { + thumbUpload = await sharp(downImage, {animated: false}).toBuffer(); + } else if (mime === "image/avif-sequence") { + thumbUpload = await sharp(downImage, {animated: false}).toBuffer(); + } else { + thumbUpload = await sharp(downImage, {animated: false}).webp({quality: 50}).toBuffer(); + } + } + + } else { + imageUpload = await resizedImage.clone().webp({quality: 60, effort: 6}).toBuffer(); + thumbUpload = await resizedImage.webp({quality: 50}).toBuffer(); + mime = "image/webp"; + } + } if (mime === "image/avif") { imageUpload = await resizedImage.clone().avif({quality: 70}).toBuffer(); @@ -158,9 +184,15 @@ class _MatrixStickerBot { imageUpload = await resizedImage.clone().jpeg({quality: 80, chromaSubsampling: '4:4:4'}).toBuffer(); thumbUpload = await resizedImage.jpeg({quality: 60, chromaSubsampling: '4:2:0'}).toBuffer();; } - stickerEvent.contentUri = await mx.upload(imageUpload, mime); + if (imageUpload) { + stickerEvent.contentUri = await mx.upload(imageUpload, mime); + } stickerEvent.mimetype = mime; - stickerEvent.thumbMxc = await mx.upload(thumbUpload, mime); + if (thumbUpload) { + stickerEvent.thumbMxc = await mx.upload(thumbUpload, mime); + } else { + continue; + } stickerEvents.push(stickerEvent); } @@ -189,8 +221,13 @@ class _MatrixStickerBot { pack.description = "Matrix sticker pack created by " + authorDisplayName; pack.license = license.name; pack.licensePath = license.url; - if (stickerEvents.length > 0) pack.avatarUrl = stickerEvents[0].thumbMxc; - await pack.save(); + if (stickerEvents.length > 0) { + pack.avatarUrl = stickerEvents[0].thumbMxc; + await pack.save(); + } else { + LogService.error("MatrixStickerBot", `No stickers in pack ${pack.name}. Removing...`); + pack.destroy(); + } const existingStickers = await Sticker.findAll({where: {packId: pack.id}}); for (const sticker of existingStickers) await sticker.destroy();