feat: Use height and width from downloaded Telegram sticker

This commit is contained in:
Tony Stipanic 2020-06-06 02:09:26 +02:00
parent 9e2393ceca
commit a768076c03
No known key found for this signature in database
GPG Key ID: 3026BCCB6C9CC6BD

View File

@ -85,16 +85,18 @@ export class AdminStickerService {
for (const tgSticker of tgPack.stickers) { for (const tgSticker of tgPack.stickers) {
LogService.info("AdminStickerService", "Importing sticker from " + tgSticker.url); LogService.info("AdminStickerService", "Importing sticker from " + tgSticker.url);
const buffer = await mx.downloadFromUrl(tgSticker.url); const buffer = await mx.downloadFromUrl(tgSticker.url);
const png = await sharp(buffer).resize({ const image = await sharp(buffer);
width: 512, const metadata = await image.metadata();
height: 512, const png = await image.resize({
width: metadata.width,
height: metadata.height,
fit: 'contain', fit: 'contain',
background: 'rgba(0,0,0,0)', background: 'rgba(0,0,0,0)',
}).png().toBuffer(); }).png().toBuffer();
const mxc = await mx.upload(png, "image/png"); const mxc = await mx.upload(png, "image/png");
const serverName = mxc.substring("mxc://".length).split("/")[0]; const serverName = mxc.substring("mxc://".length).split("/")[0];
const contentId = mxc.substring("mxc://".length).split("/")[1]; const contentId = mxc.substring("mxc://".length).split("/")[1];
const thumbMxc = await mx.uploadFromUrl(await mx.getThumbnailUrl(serverName, contentId, 512, 512, "scale", false), "image/png"); const thumbMxc = await mx.uploadFromUrl(await mx.getThumbnailUrl(serverName, contentId, metadata.width, metadata.height, "scale", false), "image/png");
stickers.push(await Sticker.create({ stickers.push(await Sticker.create({
packId: pack.id, packId: pack.id,
@ -102,8 +104,8 @@ export class AdminStickerService {
description: tgSticker.emoji, description: tgSticker.emoji,
imageMxc: mxc, imageMxc: mxc,
thumbnailMxc: thumbMxc, thumbnailMxc: thumbMxc,
thumbnailWidth: 512, thumbnailWidth: metadata.width,
thumbnailHeight: 512, thumbnailHeight: metadata.height,
mimetype: "image/png", mimetype: "image/png",
})); }));