mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
fix(media): revoke decrypted media blob URLs that outlive their component
This commit is contained in:
8 files changed
+115
-43
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
# Free decrypted images and media from memory instead of holding them until reload
|
||||
@@ -28,7 +28,7 @@ import {
|
||||
} from '$utils/matrix';
|
||||
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { useCreateObjectURL } from '$hooks/useObjectURL';
|
||||
import { MEDIA_VOLUME_KEY } from '$components/media';
|
||||
import { hasControllingServiceWorker } from '$utils/platform';
|
||||
|
||||
@@ -60,6 +60,8 @@ export function AudioContent({
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
|
||||
@@ -74,15 +76,13 @@ export function AudioContent({
|
||||
const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) =>
|
||||
decryptFile(encBuf, mimeType, encInfo)
|
||||
);
|
||||
return URL.createObjectURL(fileContent);
|
||||
return createObjectURL(fileContent);
|
||||
}
|
||||
const fileContent = await downloadMedia(mediaUrl);
|
||||
return URL.createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo])
|
||||
return createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
|
||||
);
|
||||
|
||||
useRevokeObjectURL(srcState.status === AsyncStatus.Success ? srcState.data : undefined);
|
||||
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
} from '$utils/mimeTypes';
|
||||
import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp } from '$utils/matrix';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { useCreateObjectURL } from '$hooks/useObjectURL';
|
||||
import { useDismissOnBack } from '$utils/androidBack';
|
||||
import { ModalWide } from '$styles/Modal.css';
|
||||
import { getDownloadFilename, saveFileToDevice } from '$utils/download';
|
||||
@@ -154,6 +154,8 @@ export function ReadPdfFile({ body, mimeType, url, encInfo, renderViewer }: Read
|
||||
// Android back closes the PDF viewer instead of navigating away.
|
||||
useDismissOnBack(() => setPdfViewer(false), pdfViewer);
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [pdfState, loadPdf] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
|
||||
@@ -162,12 +164,10 @@ export function ReadPdfFile({ body, mimeType, url, encInfo, renderViewer }: Read
|
||||
? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo))
|
||||
: await downloadMedia(mediaUrl);
|
||||
setPdfViewer(true);
|
||||
return URL.createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo])
|
||||
return createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
|
||||
);
|
||||
|
||||
useRevokeObjectURL(pdfState.status === AsyncStatus.Success ? pdfState.data : undefined);
|
||||
|
||||
return (
|
||||
<>
|
||||
{pdfState.status === AsyncStatus.Success && (
|
||||
@@ -223,6 +223,8 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [downloadState, download] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
|
||||
@@ -231,14 +233,12 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
|
||||
? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo))
|
||||
: await downloadMedia(mediaUrl);
|
||||
|
||||
const fileURL = URL.createObjectURL(fileContent);
|
||||
const fileURL = createObjectURL(fileContent);
|
||||
await saveFileToDevice(fileContent, getDownloadFilename(body), mimeType);
|
||||
return fileURL;
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo, body])
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo, body, createObjectURL])
|
||||
);
|
||||
|
||||
useRevokeObjectURL(downloadState.status === AsyncStatus.Success ? downloadState.data : undefined);
|
||||
|
||||
return downloadState.status === AsyncStatus.Error ? (
|
||||
renderErrorButton(download, `Retry Download (${bytesToSize(info.size ?? 0)})`)
|
||||
) : (
|
||||
|
||||
@@ -52,7 +52,7 @@ import {
|
||||
} from '../../../../unstable/prefixes';
|
||||
import { useFavoriteGifs } from '$hooks/useFavoriteGifs';
|
||||
import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { useCreateObjectURL } from '$hooks/useObjectURL';
|
||||
import { ModalOverlay } from '$components/modal-overlay/ModalOverlay';
|
||||
|
||||
export function checkIfGif(url: string, mimetype?: string, body?: string) {
|
||||
@@ -155,6 +155,8 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
|
||||
const resolvedMediaUrl = useRenderableMediaUrl(encInfo ? undefined : rawMediaUrl);
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
if (encInfo) {
|
||||
@@ -166,10 +168,10 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
const fileContent = await downloadEncryptedMedia(rawMediaUrl, (encBuf) =>
|
||||
decryptFile(encBuf, mimeType ?? FALLBACK_MIMETYPE, encInfo)
|
||||
);
|
||||
return URL.createObjectURL(fileContent);
|
||||
return createObjectURL(fileContent);
|
||||
}
|
||||
return resolvedMediaUrl ?? rawMediaUrl ?? url;
|
||||
}, [rawMediaUrl, resolvedMediaUrl, url, mimeType, encInfo])
|
||||
}, [rawMediaUrl, resolvedMediaUrl, url, mimeType, encInfo, createObjectURL])
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -213,10 +215,6 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
if (autoPlay) loadSrc().catch(() => undefined);
|
||||
}, [autoPlay, loadSrc]);
|
||||
|
||||
useRevokeObjectURL(
|
||||
encInfo && srcState.status === AsyncStatus.Success ? srcState.data : undefined
|
||||
);
|
||||
|
||||
const imageW = info?.w;
|
||||
const imageH = info?.h;
|
||||
const hasDimensions = typeof imageW === 'number' && typeof imageH === 'number';
|
||||
|
||||
@@ -14,7 +14,7 @@ import { setMediaEncryption } from '$utils/tauriMediaEncryption';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { useCreateObjectURL } from '$hooks/useObjectURL';
|
||||
import { FALLBACK_MIMETYPE } from '$utils/mimeTypes';
|
||||
|
||||
export type ThumbnailContentProps = {
|
||||
@@ -35,6 +35,8 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
|
||||
|
||||
const resolvedMediaUrl = useRenderableMediaUrl(encInfo ? undefined : rawMediaUrl);
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [thumbSrcState, loadThumbSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
const thumbInfo = info.thumbnail_info;
|
||||
@@ -52,10 +54,10 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
|
||||
const fileContent = await downloadEncryptedMedia(rawMediaUrl, (encBuf) =>
|
||||
decryptFile(encBuf, thumbInfo?.mimetype ?? FALLBACK_MIMETYPE, encInfo)
|
||||
);
|
||||
return URL.createObjectURL(fileContent);
|
||||
return createObjectURL(fileContent);
|
||||
}
|
||||
return resolvedMediaUrl ?? rawMediaUrl ?? thumbMxcUrl;
|
||||
}, [info, thumbMxcUrl, rawMediaUrl, resolvedMediaUrl, encInfo])
|
||||
}, [info, thumbMxcUrl, rawMediaUrl, resolvedMediaUrl, encInfo, createObjectURL])
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -64,10 +66,6 @@ export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
|
||||
loadThumbSrc().catch(() => undefined);
|
||||
}, [loadThumbSrc]);
|
||||
|
||||
useRevokeObjectURL(
|
||||
encInfo && thumbSrcState.status === AsyncStatus.Success ? thumbSrcState.data : undefined
|
||||
);
|
||||
|
||||
if (thumbSrcState.status === AsyncStatus.Success) return renderImage(thumbSrcState.data);
|
||||
|
||||
if (thumbSrcState.status === AsyncStatus.Loading) {
|
||||
|
||||
@@ -32,7 +32,7 @@ import {
|
||||
} from '$utils/matrix';
|
||||
import { setMediaEncryption } from '$utils/tauriMediaEncryption';
|
||||
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { useRevokeObjectURL } from '$hooks/useObjectURL';
|
||||
import { useCreateObjectURL } from '$hooks/useObjectURL';
|
||||
import { validBlurHash } from '$utils/blurHash';
|
||||
import * as css from './style.css';
|
||||
import { MATRIX_UNSTABLE_BLUR_HASH_PROPERTY_NAME } from '../../../../unstable/prefixes';
|
||||
@@ -86,6 +86,8 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
const [blurred, setBlurred] = useState(markedAsSpoiler ?? false);
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
|
||||
const createObjectURL = useCreateObjectURL();
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
if (url.startsWith('http')) return url;
|
||||
@@ -102,11 +104,11 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) =>
|
||||
decryptFile(encBuf, mimeType, encInfo)
|
||||
);
|
||||
return URL.createObjectURL(fileContent);
|
||||
return createObjectURL(fileContent);
|
||||
}
|
||||
const fileContent = await downloadMedia(mediaUrl);
|
||||
return URL.createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo])
|
||||
return createObjectURL(fileContent);
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo, createObjectURL])
|
||||
);
|
||||
|
||||
// When the source download succeeds, reset video-element error state so the
|
||||
@@ -145,8 +147,6 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
if (autoPlay) loadSrc().catch(() => undefined);
|
||||
}, [autoPlay, loadSrc]);
|
||||
|
||||
useRevokeObjectURL(srcState.status === AsyncStatus.Success ? srcState.data : undefined);
|
||||
|
||||
return (
|
||||
<Box
|
||||
className={classNames(css.RelativeBase, className)}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useCreateObjectURL } from './useObjectURL';
|
||||
|
||||
describe('useCreateObjectURL', () => {
|
||||
let created: number;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
created = 0;
|
||||
vi.spyOn(URL, 'createObjectURL').mockImplementation(() => {
|
||||
created += 1;
|
||||
return `blob:url-${created}`;
|
||||
});
|
||||
vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
it('revokes the owned url on unmount', () => {
|
||||
const { result, unmount } = renderHook(() => useCreateObjectURL());
|
||||
result.current(new Blob(['a']));
|
||||
|
||||
expect(URL.revokeObjectURL).not.toHaveBeenCalled();
|
||||
unmount();
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:url-1');
|
||||
});
|
||||
|
||||
it('revokes the previous url when a new one replaces it', () => {
|
||||
const { result } = renderHook(() => useCreateObjectURL());
|
||||
result.current(new Blob(['a']));
|
||||
result.current(new Blob(['b']));
|
||||
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledExactlyOnceWith('blob:url-1');
|
||||
});
|
||||
|
||||
it('revokes immediately when the blob resolves after unmount', () => {
|
||||
const { result, unmount } = renderHook(() => useCreateObjectURL());
|
||||
unmount();
|
||||
|
||||
result.current(new Blob(['late']));
|
||||
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:url-1');
|
||||
});
|
||||
|
||||
it('keeps a stable identity so it can sit in effect dependencies', () => {
|
||||
const { result, rerender } = renderHook(() => useCreateObjectURL());
|
||||
const first = result.current;
|
||||
rerender();
|
||||
|
||||
expect(result.current).toBe(first);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||
|
||||
export const useObjectURL = (object?: Blob): string | undefined => {
|
||||
const url = useMemo(() => {
|
||||
@@ -16,11 +16,30 @@ export const useObjectURL = (object?: Blob): string | undefined => {
|
||||
return url;
|
||||
};
|
||||
|
||||
export const useRevokeObjectURL = (url?: string): void => {
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (url?.startsWith('blob:')) URL.revokeObjectURL(url);
|
||||
},
|
||||
[url]
|
||||
);
|
||||
// For blob URLs created inside an async callback, which can resolve after unmount
|
||||
// with no effect left to revoke them. Owns one URL at a time per caller.
|
||||
export const useCreateObjectURL = (): ((object: Blob) => string) => {
|
||||
const urlRef = useRef<string | undefined>(undefined);
|
||||
const mountedRef = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true;
|
||||
return () => {
|
||||
mountedRef.current = false;
|
||||
if (urlRef.current) URL.revokeObjectURL(urlRef.current);
|
||||
urlRef.current = undefined;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return useCallback((object: Blob) => {
|
||||
const url = URL.createObjectURL(object);
|
||||
if (urlRef.current) URL.revokeObjectURL(urlRef.current);
|
||||
if (!mountedRef.current) {
|
||||
URL.revokeObjectURL(url);
|
||||
urlRef.current = undefined;
|
||||
return url;
|
||||
}
|
||||
urlRef.current = url;
|
||||
return url;
|
||||
}, []);
|
||||
};
|
||||
Reference in new issue
Block a user