Fix downloading media for WhatsApp Web v2.2126.10 (#735)

* fix downloadMedia

* return a boolean for hasMedia
This commit is contained in:
Pedro S. Lopez
2021-07-09 02:38:31 -04:00
committed by GitHub
parent 0465507009
commit 0ef6061d7e
2 changed files with 21 additions and 43 deletions

View File

@@ -41,7 +41,7 @@ class Message extends Base {
* Indicates if the message has media available for download
* @type {boolean}
*/
this.hasMedia = data.clientUrl || data.deprecatedMms3Url;
this.hasMedia = Boolean(data.mediaKey && data.directPath);
/**
* Message content
@@ -312,14 +312,20 @@ class Message extends Base {
return undefined;
}
const mediaUrl = msg.clientUrl || msg.deprecatedMms3Url;
const decryptedMedia = await window.Store.DownloadManager.downloadAndDecrypt({
directPath: msg.directPath,
encFilehash: msg.encFilehash,
filehash: msg.filehash,
mediaKey: msg.mediaKey,
mediaKeyTimestamp: msg.mediaKeyTimestamp,
type: msg.type,
signal: (new AbortController).signal
});
const buffer = await window.WWebJS.downloadBuffer(mediaUrl);
const decrypted = await window.Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await window.WWebJS.readBlobAsync(decrypted._blob);
const data = window.WWebJS.arrayBufferToBase64(decryptedMedia);
return {
data: data.split(',')[1],
data,
mimetype: msg.mimetype,
filename: msg.filename
};

View File

@@ -35,6 +35,7 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.Features = window.mR.findModule('FEATURE_CHANGE_EVENT')[0].default;
window.Store.QueryOrder = window.mR.findModule('queryOrder')[0];
window.Store.QueryProduct = window.mR.findModule('queryProduct')[0];
window.Store.DownloadManager = window.mR.findModule('DownloadManager')[0].default;
};
exports.LoadUtils = () => {
@@ -349,43 +350,14 @@ exports.LoadUtils = () => {
});
};
window.WWebJS.downloadBuffer = (url) => {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (xhr.status == 200) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(null);
});
};
window.WWebJS.readBlobAsync = (blob) => {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsDataURL(blob);
});
window.WWebJS.arrayBufferToBase64 = async (arrayBuffer) => {
let binary = '';
const bytes = new Uint8Array( arrayBuffer );
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
};
window.WWebJS.getFileHash = async (data) => {