From 8462d7d38efd9dd5b17f68bd34a4a3694e9f7ad4 Mon Sep 17 00:00:00 2001 From: audric Date: Thu, 2 Sep 2021 10:33:42 +1000 Subject: [PATCH] make encrypted content fast load if already stored decrypted --- ts/hooks/useEncryptedFileFetch.ts | 15 +++++++++++-- .../crypto/DecryptedAttachmentsManager.ts | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ts/hooks/useEncryptedFileFetch.ts b/ts/hooks/useEncryptedFileFetch.ts index 0742525a4..b71f51168 100644 --- a/ts/hooks/useEncryptedFileFetch.ts +++ b/ts/hooks/useEncryptedFileFetch.ts @@ -1,12 +1,15 @@ import { useEffect, useRef, useState } from 'react'; -import { getDecryptedMediaUrl } from '../session/crypto/DecryptedAttachmentsManager'; +import { + getAlreadyDecryptedMediaUrl, + getDecryptedMediaUrl, +} from '../session/crypto/DecryptedAttachmentsManager'; import { perfEnd, perfStart } from '../session/utils/Performance'; export const useEncryptedFileFetch = (url: string, contentType: string) => { // tslint:disable-next-line: no-bitwise const [urlToLoad, setUrlToLoad] = useState(''); - const [loading, setLoading] = useState(true); + const [loading, setLoading] = useState(false); const mountedRef = useRef(true); @@ -22,8 +25,12 @@ export const useEncryptedFileFetch = (url: string, contentType: string) => { setLoading(false); } } + const alreadyDecrypted = getAlreadyDecryptedMediaUrl(url); useEffect(() => { + if (alreadyDecrypted) { + return; + } setLoading(true); mountedRef.current = true; void fetchUrl(); @@ -32,5 +39,9 @@ export const useEncryptedFileFetch = (url: string, contentType: string) => { mountedRef.current = false; }; }, [url]); + + if (alreadyDecrypted) { + return { urlToLoad: alreadyDecrypted, loading: false }; + } return { urlToLoad, loading }; }; diff --git a/ts/session/crypto/DecryptedAttachmentsManager.ts b/ts/session/crypto/DecryptedAttachmentsManager.ts index 833a471c5..5e578d291 100644 --- a/ts/session/crypto/DecryptedAttachmentsManager.ts +++ b/ts/session/crypto/DecryptedAttachmentsManager.ts @@ -105,3 +105,25 @@ export const getDecryptedMediaUrl = async (url: string, contentType: string): Pr return url; } }; + +/** + * + * Returns the already decrypted URL or null + */ +export const getAlreadyDecryptedMediaUrl = (url: string): string | null => { + if (!url) { + return null; + } + if (url.startsWith('blob:')) { + return url; + } else if ( + window.Signal.Migrations.attachmentsPath && + url.startsWith(window.Signal.Migrations.attachmentsPath) + ) { + if (urlToDecryptedBlobMap.has(url)) { + const existingObjUrl = urlToDecryptedBlobMap.get(url)?.decrypted as string; + return existingObjUrl; + } + } + return null; +};