Skip to content

Commit

Permalink
Merge pull request #2 from mpirescarvalho/refactor
Browse files Browse the repository at this point in the history
Move some code
  • Loading branch information
PurpShell committed Feb 2, 2021
2 parents 46836a7 + fe3648d commit f421cd7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
27 changes: 6 additions & 21 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 40 additions & 4 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageMedia>} 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
}

/**
Expand Down

0 comments on commit f421cd7

Please sign in to comment.