diff --git a/src/Client.js b/src/Client.js index 26d33a3dd..1de357a78 100644 --- a/src/Client.js +++ b/src/Client.js @@ -479,28 +479,13 @@ class Client extends EventEmitter { } if (internalOptions.sendMediaAsSticker && internalOptions.attachment) { - internalOptions.attachment = await Util.formatToWebpSticker(internalOptions.attachment); - if (options.stickerName || options.stickerAuthor) { - const exifPath = 'data.exif'; - const resultPath = 'sticker.webp'; - internalOptions.attachment = await Util.formatToWebpSticker(internalOptions.attachment); - fs.writeFileSync(resultPath, internalOptions.attachment.data, 'base64'); - const random_id = Math.floor(Math.random() * (9999999999 - 1000000000) + 1000000000); - const stickerpackid = 'com.marsvard.stickermakerforwhatsapp.stickercontentprovider '+ random_id; - const packname = options.stickerName || 'undefined'; - const author = options.stickerAuthor || 'undefined'; - const googlelink = 'https://play.google.com/store/apps/details?id=com.marsvard.stickermakerforwhatsapp'; - const applelink = 'https://itunes.apple.com/app/sticker-maker-studio/id1443326857'; - const json = { 'sticker-pack-id': stickerpackid, 'sticker-pack-name': packname, 'sticker-pack-publisher': author, 'android-app-store-link': googlelink, 'ios-app-store-link': applelink }; - let exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00]); - let jsonBuffer = Buffer.from(JSON.stringify(json), 'utf8'); - let exif = Buffer.concat([exifAttr, jsonBuffer]); - exif.writeUIntLE(jsonBuffer.length, 14, 4); - fs.writeFileSync(exifPath, exif); - webp.webpmux_add(resultPath, resultPath, exifPath, 'exif'); - internalOptions.attachment = await MessageMedia.fromFilePath(resultPath); - } + internalOptions.attachment = + await Util.formatToWebpSticker(internalOptions.attachment, { + name: options.stickerName, + author: options.stickerAuthor + }); } + const newMessage = await this.pupPage.evaluate(async (chatId, message, options, sendSeen) => { const chatWid = window.Store.WidFactory.createWid(chatId); const chat = await window.Store.Chat.find(chatWid); diff --git a/src/util/Util.js b/src/util/Util.js index ec245e1d4..9e6712363 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -135,16 +135,52 @@ class Util { }; } + /** + * Sticker metadata. + * @typedef {Object} StickerMetadata + * @property {string} [name] + * @property {string} [author] + */ + /** * Formats a media to webp * @param {MessageMedia} media + * @param {StickerMetadata} metadata * * @returns {Promise} media in webp format */ - static async formatToWebpSticker(media) { - if (media.mimetype.includes('image')) return this.formatImageToWebpSticker(media); - else if (media.mimetype.includes('video')) return this.formatVideoToWebpSticker(media); - else throw new Error('Invalid media format'); + static async formatToWebpSticker(media, metadata) { + let webpMedia + + if (media.mimetype.includes('image')) + webpMedia = this.formatImageToWebpSticker(media); + else if (media.mimetype.includes('video')) + webMedia = this.formatVideoToWebpSticker(media); + else + throw new Error('Invalid media format'); + + + if (metadata.name || metadata.author) { + const exifPath = 'data.exif'; + const resultPath = 'sticker.webp'; + fs.writeFileSync(resultPath, webpMedia.data, 'base64'); + const random_id = Math.floor(Math.random() * (9999999999 - 1000000000) + 1000000000); + const stickerpackid = 'com.marsvard.stickermakerforwhatsapp.stickercontentprovider '+ random_id; + const packname = metadata.name || 'undefined'; + const author = metadata.author || 'undefined'; + const googlelink = 'https://play.google.com/store/apps/details?id=com.marsvard.stickermakerforwhatsapp'; + const applelink = 'https://itunes.apple.com/app/sticker-maker-studio/id1443326857'; + const json = { 'sticker-pack-id': stickerpackid, 'sticker-pack-name': packname, 'sticker-pack-publisher': author, 'android-app-store-link': googlelink, 'ios-app-store-link': applelink }; + let exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00]); + let jsonBuffer = Buffer.from(JSON.stringify(json), 'utf8'); + let exif = Buffer.concat([exifAttr, jsonBuffer]); + exif.writeUIntLE(jsonBuffer.length, 14, 4); + fs.writeFileSync(exifPath, exif); + webp.webpmux_add(resultPath, resultPath, exifPath, 'exif'); + webpMedia = await MessageMedia.fromFilePath(resultPath); + } + + return webpMedia } /**