Download message attachments (images, documents, etc)

This commit is contained in:
Pedro Lopez
2019-11-24 04:23:08 -04:00
parent dfcf76c139
commit 64912dccfd
4 changed files with 74 additions and 6 deletions

View File

@@ -15,7 +15,8 @@ class Message extends Base {
_patch(data) {
this.id = data.id;
this.body = data.body;
this.hasMedia = data.clientUrl ? true : false;
this.body = this.hasMedia ? data.caption || '' : data.body || '';
this.type = data.type;
this.timestamp = data.t;
this.from = data.from;
@@ -60,6 +61,26 @@ class Message extends Base {
}, chatId, this.id._serialized, message);
}
async downloadMedia() {
if (!this.hasMedia) {
return undefined;
}
return await this.client.pupPage.evaluate(async (msgId) => {
const msg = Store.Msg.get(msgId);
const buffer = await WWebJS.downloadBuffer(msg.clientUrl);
const decrypted = await Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await WWebJS.readBlobAsync(decrypted._blob);
return {
data,
mimetype: msg.mimetype,
filename: msg.filename
}
}, this.id._serialized);
}
}
module.exports = Message;