diff --git a/app/containers/message/hooks/useMediaAutoDownload.tsx b/app/containers/message/hooks/useMediaAutoDownload.tsx index e7d7c60609..f5ddebfe53 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) { + await persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url); + } + updateCurrentFile(result.uri); } return result?.exists; diff --git a/app/lib/methods/handleMediaDownload.ts b/app/lib/methods/handleMediaDownload.ts index c1bcc2b73d..0e86c7cd03 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: att.image_url && !downloadUrl.startsWith("file://") && downloadUrl.includes(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 }); }) ); } @@ -270,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(); @@ -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); @@ -294,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