From d224857cf28908c9531ffa2ff3b30cfb4f1dad3a Mon Sep 17 00:00:00 2001 From: Rohit Bansal <40559587+Rohit3523@users.noreply.github.com> Date: Fri, 8 Nov 2024 03:44:02 +0530 Subject: [PATCH 1/6] Cache overwrite when having multiple images --- app/lib/methods/handleMediaDownload.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index c1bcc2b73d..849f3b8d21 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -202,26 +202,28 @@ export async function cancelDownload(messageUrl: string): Promise { const mapAttachments = ({ attachments, uri, - encryption + encryption, + downloadUrl }: { attachments?: IAttachment[]; uri: string; encryption: boolean; + downloadUrl: string; }): TMessageModel['attachments'] => attachments?.map(att => ({ ...att, - title_link: uri, + title_link: downloadUrl === att.image_url ? uri : att.title_link, e2e: encryption ? 'done' : undefined })); -const persistMessage = async (messageId: string, uri: string, encryption: boolean) => { +const persistMessage = async (messageId: string, uri: string, encryption: boolean, downloadUrl: string) => { const db = database.active; const batch: Model[] = []; const messageRecord = await getMessageById(messageId); if (messageRecord) { batch.push( messageRecord.prepareUpdate(m => { - m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption }); + m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl }); }) ); } @@ -229,7 +231,7 @@ const persistMessage = async (messageId: string, uri: string, encryption: boolea if (threadRecord) { batch.push( threadRecord.prepareUpdate(m => { - m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption }); + m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl }); }) ); } @@ -237,7 +239,7 @@ const persistMessage = async (messageId: string, uri: string, encryption: boolea if (threadMessageRecord) { batch.push( threadMessageRecord.prepareUpdate(m => { - m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption }); + m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl }); }) ); } @@ -282,7 +284,7 @@ export function downloadMediaFile({ await Encryption.addFileToDecryptFileQueue(messageId, result.uri, encryption, originalChecksum); } - await persistMessage(messageId, result.uri, !!encryption); + await persistMessage(messageId, result.uri, !!encryption, downloadUrl); emitter.emit(`downloadMedia${downloadUrl}`, result.uri); return resolve(result.uri); From 31ec0c987e8a3f5dc9994ffac783e436268880ce Mon Sep 17 00:00:00 2001 From: Rohit Bansal <40559587+Rohit3523@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:05:20 +0530 Subject: [PATCH 2/6] Fixed a condition where check fail because download url contains signed parameters --- app/lib/methods/handleMediaDownload.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index 849f3b8d21..5e6408cfc4 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -212,7 +212,7 @@ const mapAttachments = ({ }): TMessageModel['attachments'] => attachments?.map(att => ({ ...att, - title_link: downloadUrl === att.image_url ? uri : att.title_link, + title_link: att.image_url && downloadUrl.includes(att.image_url) ? uri : att.title_link, e2e: encryption ? 'done' : undefined })); From a6237afff8b30169d15328cd1b89de08d723f714 Mon Sep 17 00:00:00 2001 From: Rohit Bansal <40559587+Rohit3523@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:25:55 +0530 Subject: [PATCH 3/6] Show attachment when title link is missing --- app/containers/message/hooks/useMediaAutoDownload.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/containers/message/hooks/useMediaAutoDownload.tsx b/app/containers/message/hooks/useMediaAutoDownload.tsx index e7d7c60609..69c817d724 100644 --- a/app/containers/message/hooks/useMediaAutoDownload.tsx +++ b/app/containers/message/hooks/useMediaAutoDownload.tsx @@ -150,7 +150,7 @@ export const useMediaAutoDownload = ({ download(); return; } - if (!showAttachment || !currentFile.title_link || isEncrypted) { + if (!showAttachment || isEncrypted) { return; } showAttachment(currentFile); From b5af2877388ae70e971af2f70c080a63a96a13e0 Mon Sep 17 00:00:00 2001 From: Rohit Bansal <40559587+Rohit3523@users.noreply.github.com> Date: Sat, 9 Nov 2024 02:52:34 +0530 Subject: [PATCH 4/6] Show image from cache and update db if local file uri is missing (same image send multiple times) --- app/containers/message/hooks/useMediaAutoDownload.tsx | 7 ++++++- app/lib/methods/handleMediaDownload.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/containers/message/hooks/useMediaAutoDownload.tsx b/app/containers/message/hooks/useMediaAutoDownload.tsx index 69c817d724..acdef91ef9 100644 --- a/app/containers/message/hooks/useMediaAutoDownload.tsx +++ b/app/containers/message/hooks/useMediaAutoDownload.tsx @@ -9,6 +9,7 @@ import { getMediaCache, isDownloadActive, MediaTypes, + persistMessageWithCacheFile, TDownloadState } from '../../../lib/methods/handleMediaDownload'; import { emitter } from '../../../lib/methods/helpers'; @@ -135,6 +136,10 @@ export const useMediaAutoDownload = ({ urlToCache: url }); if (result?.exists && !isEncrypted) { + if(!currentFile.title_link) { + persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url); + } + updateCurrentFile(result.uri); } return result?.exists; @@ -150,7 +155,7 @@ export const useMediaAutoDownload = ({ download(); return; } - if (!showAttachment || isEncrypted) { + if (!showAttachment || !currentFile.title_link || isEncrypted) { return; } showAttachment(currentFile); diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index 5e6408cfc4..0e86c7cd03 100644 --- a/app/lib/methods/handleMediaDownload.ts +++ b/app/lib/methods/handleMediaDownload.ts @@ -212,7 +212,7 @@ const mapAttachments = ({ }): TMessageModel['attachments'] => attachments?.map(att => ({ ...att, - title_link: att.image_url && downloadUrl.includes(att.image_url) ? uri : att.title_link, + title_link: att.image_url && !downloadUrl.startsWith("file://") && downloadUrl.includes(att.image_url) ? uri : att.title_link, e2e: encryption ? 'done' : undefined })); @@ -272,7 +272,7 @@ export function downloadMediaFile({ if (!path) { return reject(); } - downloadKey = mediaDownloadKey(downloadUrl); + downloadKey = messageId + mediaDownloadKey(downloadUrl); downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path); const result = await downloadQueue[downloadKey].downloadAsync(); @@ -296,3 +296,7 @@ export function downloadMediaFile({ } }); } + +export const persistMessageWithCacheFile = async (messageId: string, uri: string, encryption: TAttachmentEncryption | undefined, downloadUrl: string) => { + await persistMessage(messageId, uri, !!encryption, downloadUrl); +} \ No newline at end of file From 300e5afd0225e5c5fce7d17966ef98446c54e08f Mon Sep 17 00:00:00 2001 From: Rohit Bansal <40559587+Rohit3523@users.noreply.github.com> Date: Sat, 9 Nov 2024 02:53:11 +0530 Subject: [PATCH 5/6] Just to make sure title_link is present --- app/containers/message/hooks/useMediaAutoDownload.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/containers/message/hooks/useMediaAutoDownload.tsx b/app/containers/message/hooks/useMediaAutoDownload.tsx index acdef91ef9..a884a6941b 100644 --- a/app/containers/message/hooks/useMediaAutoDownload.tsx +++ b/app/containers/message/hooks/useMediaAutoDownload.tsx @@ -136,7 +136,7 @@ export const useMediaAutoDownload = ({ urlToCache: url }); if (result?.exists && !isEncrypted) { - if(!currentFile.title_link) { + if(!currentFile?.title_link) { persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url); } From fe93aea224be1fb9fe3d3fef1936f9244a1d489d Mon Sep 17 00:00:00 2001 From: OtavioStasiak Date: Tue, 12 Nov 2024 12:26:56 -0300 Subject: [PATCH 6/6] fix: added await on checkCache --- app/containers/message/hooks/useMediaAutoDownload.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/containers/message/hooks/useMediaAutoDownload.tsx b/app/containers/message/hooks/useMediaAutoDownload.tsx index a884a6941b..f5ddebfe53 100644 --- a/app/containers/message/hooks/useMediaAutoDownload.tsx +++ b/app/containers/message/hooks/useMediaAutoDownload.tsx @@ -136,10 +136,10 @@ export const useMediaAutoDownload = ({ urlToCache: url }); if (result?.exists && !isEncrypted) { - if(!currentFile?.title_link) { - persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url); + if (!currentFile?.title_link) { + await persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url); } - + updateCurrentFile(result.uri); } return result?.exists;