From d9ccb6f276a1308ab199b91463768290757fbc99 Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sun, 1 Aug 2021 23:29:06 -0400 Subject: [PATCH] fix: correctly resolve media that hasn't been downloaded (#763) * fix: correctly abort if media does not exist * catch 404s * dont throw for media that cant be fetched --- src/structures/Message.js | 47 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index d5c1e4f..1930ff1 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -313,32 +313,39 @@ class Message extends Base { if (msg.mediaData.mediaStage != 'RESOLVED') { // try to resolve media - await msg.downloadMedia(true, 1); + await msg.downloadMedia({ + downloadEvenIfExpensive: true, + rmrReason: 1 + }); } - if (msg.mediaData.mediaStage.includes('ERROR')) { + if (msg.mediaData.mediaStage.includes('ERROR') || msg.mediaData.mediaStage === 'FETCHING') { // media could not be downloaded return undefined; } - 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 data = window.WWebJS.arrayBufferToBase64(decryptedMedia); - - return { - data, - mimetype: msg.mimetype, - filename: msg.filename - }; - + try { + 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 data = window.WWebJS.arrayBufferToBase64(decryptedMedia); + + return { + data, + mimetype: msg.mimetype, + filename: msg.filename + }; + } catch (e) { + if(e.status && e.status === 404) return undefined; + throw e; + } }, this.id._serialized); if (!result) return undefined;