From 99973c0d286169d6458d6dac0e6c7825b12ee93f Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Fri, 19 May 2023 11:35:36 -0300 Subject: [PATCH] fix the try catch download media --- app/containers/message/Audio.tsx | 26 ++++++++++--------- app/containers/message/Image.tsx | 35 ++++++++++++++------------ app/containers/message/Video.tsx | 19 +++++++------- app/lib/methods/handleMediaDownload.ts | 2 +- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/app/containers/message/Audio.tsx b/app/containers/message/Audio.tsx index cf2abb2d9..fe2224022 100644 --- a/app/containers/message/Audio.tsx +++ b/app/containers/message/Audio.tsx @@ -300,19 +300,21 @@ class MessageAudio extends React.Component { setLoading(true); - // The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl - // And we don't have sure that exists the file.title_link - const imgUrl = imageUrl || formatAttachmentUrl(newFile.title_link || newFile.image_url, user.id, user.token, baseUrl); - const imageUri = await downloadMediaFile({ - downloadUrl: imgUrl, - mediaType: MediaTypes.image, - messageId, - path: filePath.current - }); - if (!imageUri) { + try { + // The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl + // And we don't have sure that exists the file.title_link + const imgUrl = imageUrl || formatAttachmentUrl(newFile.title_link || newFile.image_url, user.id, user.token, baseUrl); + const imageUri = await downloadMediaFile({ + downloadUrl: imgUrl, + mediaType: MediaTypes.image, + messageId, + path: filePath.current + }); + + setNewFile(prev => ({ + ...prev, + title_link: imageUri + })); + setToDownload(false); + setLoading(false); + } catch (e) { + console.log('🚀 ~ file: Image.tsx:164 ~ handleDownload ~ e:', e); setLoading(false); return setToDownload(true); } - setNewFile(prev => ({ - ...prev, - title_link: filePath.current - })); - setToDownload(false); - setLoading(false); }; const onPress = () => { diff --git a/app/containers/message/Video.tsx b/app/containers/message/Video.tsx index c82050c8b..34e635a9a 100644 --- a/app/containers/message/Video.tsx +++ b/app/containers/message/Video.tsx @@ -123,19 +123,20 @@ const Video = React.memo( const handleDownload = async () => { setLoading(true); - const videoUri = await downloadMediaFile({ - downloadUrl: video, - mediaType: MediaTypes.video, - messageId, - path: filePath.current - }); - if (videoUri) { + try { + const videoUri = await downloadMediaFile({ + downloadUrl: video, + mediaType: MediaTypes.video, + messageId, + path: filePath.current + }); setNewFile(prev => ({ ...prev, - video_url: filePath.current + video_url: videoUri })); + } finally { + setLoading(false); } - setLoading(false); }; const onPress = async () => { diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index 6ab580844..b49ecb971 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -56,7 +56,7 @@ export function downloadMediaFile({ messageId: string; downloadUrl: string; path: string; -}): Promise { +}): Promise { return new Promise((resolve, reject) => { const downloadKey = mediaDownloadKey(mediaType, messageId); const options = {