mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
fix(media): load images natively instead of through the session blob cache
This commit is contained in:
10 files changed
+4
-405
No files matched your search
@@ -106,7 +106,6 @@ export function CustomEmojiItem({
|
||||
>
|
||||
<MediaImage
|
||||
loading="lazy"
|
||||
sessionCache
|
||||
className={css.CustomEmojiImg}
|
||||
alt={image.body || image.shortcode}
|
||||
info={image.info}
|
||||
@@ -145,7 +144,6 @@ export function StickerItem({
|
||||
>
|
||||
<MediaImage
|
||||
loading="lazy"
|
||||
sessionCache
|
||||
className={css.StickerImg}
|
||||
alt={image.body || image.shortcode}
|
||||
info={image.info}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { stubObjectUrls } from '../../../test/objectUrlStub';
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({
|
||||
isTauri: () => true,
|
||||
}));
|
||||
|
||||
import { clearMediaObjectUrls } from '$utils/mediaObjectUrlCache';
|
||||
import { Image } from './Image';
|
||||
|
||||
const wrapMediaUrl = (target: string) =>
|
||||
`http://sable-media.localhost/${encodeURIComponent(target)}?__sable_media_cache=3`;
|
||||
|
||||
const THUMBNAIL_URL = wrapMediaUrl('https://example.org/_matrix/media/v3/thumbnail/a/b');
|
||||
const DOWNLOAD_URL = wrapMediaUrl('https://example.org/_matrix/media/v3/download/a/b');
|
||||
const LOOKALIKE_URL = wrapMediaUrl('https://example.org/_matrix/media/v3/download/a/thumbnail.png');
|
||||
|
||||
beforeEach(() => {
|
||||
stubObjectUrls();
|
||||
clearMediaObjectUrls();
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(() =>
|
||||
Promise.resolve(new Response('image-bytes', { status: 200 }))
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('Image on Tauri', () => {
|
||||
it('serves thumbnails from the session blob cache, fetching once across remounts', async () => {
|
||||
const first = render(<Image src={THUMBNAIL_URL} alt="thumb" />);
|
||||
await waitFor(() => expect(screen.getByAltText('thumb')).toHaveAttribute('src', 'blob:mock-1'));
|
||||
first.unmount();
|
||||
|
||||
render(<Image src={THUMBNAIL_URL} alt="thumb-again" />);
|
||||
expect(screen.getByAltText('thumb-again')).toHaveAttribute('src', 'blob:mock-1');
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps full-size downloads on the native scheme path', () => {
|
||||
render(<Image src={DOWNLOAD_URL} alt="full" />);
|
||||
|
||||
expect(screen.getByAltText('full')).toHaveAttribute('src', DOWNLOAD_URL);
|
||||
expect(fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not treat a download named "thumbnail" as a thumbnail', () => {
|
||||
render(<Image src={LOOKALIKE_URL} alt="lookalike" />);
|
||||
|
||||
expect(screen.getByAltText('lookalike')).toHaveAttribute('src', LOOKALIKE_URL);
|
||||
expect(fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('session-caches small picker downloads and skips files over the size gate', async () => {
|
||||
render(<Image src={DOWNLOAD_URL} alt="emote" sessionCache info={{ size: 20_000 }} />);
|
||||
await waitFor(() => expect(screen.getByAltText('emote')).toHaveAttribute('src', 'blob:mock-1'));
|
||||
|
||||
render(<Image src={DOWNLOAD_URL} alt="big" sessionCache info={{ size: 5_000_000 }} />);
|
||||
expect(screen.getByAltText('big')).toHaveAttribute('src', DOWNLOAD_URL);
|
||||
});
|
||||
});
|
||||
@@ -3,8 +3,6 @@ import { forwardRef, lazy, Suspense, useCallback, useEffect, useRef, useState }
|
||||
import classNames from 'classnames';
|
||||
import type { DotLottieReact as DotLottieReactComponent } from '@lottiefiles/dotlottie-react';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { useTauriMediaObjectUrl } from '$hooks/useTauriMediaObjectUrl';
|
||||
import { isThumbnailMediaUrl } from '$utils/mediaUrl';
|
||||
import { isPixelatedRendering, settingsAtom } from '$state/settings';
|
||||
import * as css from './media.css';
|
||||
import type { IImageInfo } from '$types/matrix/common';
|
||||
@@ -15,7 +13,6 @@ type ImageProps = Omit<ImgHTMLAttributes<HTMLImageElement>, 'onPointerDown'> & {
|
||||
disableDefaultSizing?: boolean;
|
||||
disablePixelation?: boolean;
|
||||
pixelated?: boolean;
|
||||
sessionCache?: boolean;
|
||||
onLottieLoad?: (canvas?: HTMLCanvasElement) => void;
|
||||
onLottieError?: () => void;
|
||||
onPointerDown?: PointerEventHandler<HTMLElement>;
|
||||
@@ -40,7 +37,6 @@ const DotLottieReact = lazy(() =>
|
||||
) as typeof DotLottieReactComponent;
|
||||
|
||||
const GZIPPED_LOTTIE_MIME = /^application\/(?:(?:x-)?gzip|x-tgsticker)(?:;|$)/i;
|
||||
const MAX_SESSION_CACHE_BYTES = 512 * 1024;
|
||||
const MAX_COMPRESSED_LOTTIE_BYTES = 1024 * 1024;
|
||||
const MAX_DECOMPRESSED_LOTTIE_BYTES = 8 * 1024 * 1024;
|
||||
const MAX_LOTTIE_DIMENSION = 4096;
|
||||
@@ -408,7 +404,6 @@ export const Image = forwardRef<HTMLImageElement | HTMLCanvasElement, ImageProps
|
||||
onLottieLoad,
|
||||
onLottieError,
|
||||
pixelated,
|
||||
sessionCache,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
@@ -463,18 +458,8 @@ export const Image = forwardRef<HTMLImageElement | HTMLCanvasElement, ImageProps
|
||||
}, [src]);
|
||||
|
||||
// A lottie candidate is not an image request until it resolves to non-animation data.
|
||||
const imageSource = resolvedLottieJson === null ? src : undefined;
|
||||
// Full media stays native: blob buffering would inflate memory and break Range streaming.
|
||||
// `info.size` describes the source file, so it cannot gate a server-scaled thumbnail.
|
||||
const thumbnail = imageSource !== undefined && isThumbnailMediaUrl(imageSource);
|
||||
const withinCacheBudget =
|
||||
thumbnail || info?.size === undefined || info.size <= MAX_SESSION_CACHE_BYTES;
|
||||
const cacheSrc =
|
||||
imageSource !== undefined && (thumbnail || sessionCache === true) && withinCacheBudget
|
||||
? imageSource
|
||||
: undefined;
|
||||
const tauriObjectSrc = useTauriMediaObjectUrl(cacheSrc);
|
||||
const renderedSrc = cacheSrc !== undefined ? tauriObjectSrc : imageSource;
|
||||
const renderedSrc = resolvedLottieJson === null ? src : undefined;
|
||||
const pending = src !== undefined && renderedSrc === undefined;
|
||||
|
||||
const shouldRenderLottie = typeof resolvedLottieJson === 'string';
|
||||
|
||||
@@ -507,8 +492,8 @@ export const Image = forwardRef<HTMLImageElement | HTMLCanvasElement, ImageProps
|
||||
loading={loading}
|
||||
decoding={decoding}
|
||||
src={renderedSrc}
|
||||
aria-busy={src !== undefined && renderedSrc === undefined ? true : undefined}
|
||||
style={style}
|
||||
aria-busy={pending ? true : undefined}
|
||||
style={pending ? { ...style, visibility: 'hidden' } : style}
|
||||
onLoad={onLoad}
|
||||
onPointerDown={onPointerDown}
|
||||
onError={(event) => {
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { stubObjectUrls } from '../../test/objectUrlStub';
|
||||
|
||||
const isTauriMock = vi.fn<() => boolean>(() => true);
|
||||
vi.mock('@tauri-apps/api/core', () => ({
|
||||
isTauri: () => isTauriMock(),
|
||||
}));
|
||||
|
||||
import { clearMediaObjectUrls, getMediaObjectUrl } from '$utils/mediaObjectUrlCache';
|
||||
import { useTauriMediaObjectUrl } from './useTauriMediaObjectUrl';
|
||||
|
||||
const SRC =
|
||||
'http://sable-media.localhost/https%3A%2F%2fexample.org%2F_matrix%2Fmedia%2Fv3%2Fdownload%2Fa%2Fb?__sable_media_cache=3';
|
||||
|
||||
const mockFetchResolve = (body = 'image-bytes') =>
|
||||
vi
|
||||
.spyOn(globalThis, 'fetch')
|
||||
.mockImplementation(() => Promise.resolve(new Response(body, { status: 200 })));
|
||||
|
||||
beforeEach(() => {
|
||||
isTauriMock.mockReturnValue(true);
|
||||
stubObjectUrls();
|
||||
clearMediaObjectUrls();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('useTauriMediaObjectUrl', () => {
|
||||
it('passes through untouched outside Tauri', async () => {
|
||||
isTauriMock.mockReturnValue(false);
|
||||
const fetchSpy = mockFetchResolve();
|
||||
|
||||
const { result } = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
|
||||
expect(result.current).toBe(SRC);
|
||||
await waitFor(() => expect(fetchSpy).not.toHaveBeenCalled());
|
||||
});
|
||||
|
||||
it('resolves to a cached object URL and fetches only once per URL', async () => {
|
||||
const fetchSpy = mockFetchResolve();
|
||||
|
||||
const first = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
expect(first.result.current).toBeUndefined();
|
||||
await waitFor(() => expect(first.result.current).toBe('blob:mock-1'));
|
||||
|
||||
const second = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
expect(second.result.current).toBe('blob:mock-1');
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('falls back to the raw URL when the fetch fails, and retries on remount', async () => {
|
||||
const fetchSpy = vi
|
||||
.spyOn(globalThis, 'fetch')
|
||||
.mockRejectedValueOnce(new Error('network down'))
|
||||
.mockResolvedValue(new Response('image-bytes', { status: 200 }));
|
||||
|
||||
const first = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
await waitFor(() => expect(first.result.current).toBe(SRC));
|
||||
first.unmount();
|
||||
|
||||
const second = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
await waitFor(() => expect(second.result.current).toBe('blob:mock-1'));
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('returns undefined until the fetch resolves', async () => {
|
||||
let release!: (response: Response) => void;
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(
|
||||
() =>
|
||||
new Promise<Response>((resolve) => {
|
||||
release = resolve;
|
||||
})
|
||||
);
|
||||
|
||||
const { result } = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
expect(result.current).toBeUndefined();
|
||||
|
||||
release(new Response('image-bytes', { status: 200 }));
|
||||
await waitFor(() => expect(result.current).toBe('blob:mock-1'));
|
||||
});
|
||||
|
||||
it('dedupes concurrent mounts of the same URL', async () => {
|
||||
const fetchSpy = mockFetchResolve();
|
||||
|
||||
const a = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
const b = renderHook(() => useTauriMediaObjectUrl(SRC));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(a.result.current).toBe('blob:mock-1');
|
||||
expect(b.result.current).toBe('blob:mock-1');
|
||||
});
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps changes of src independent', async () => {
|
||||
mockFetchResolve();
|
||||
const OTHER = `${SRC}&other=1`;
|
||||
|
||||
const { result, rerender } = renderHook(({ src }) => useTauriMediaObjectUrl(src), {
|
||||
initialProps: { src: SRC },
|
||||
});
|
||||
await waitFor(() => expect(result.current).toBe('blob:mock-1'));
|
||||
|
||||
rerender({ src: OTHER });
|
||||
expect(result.current).toBeUndefined();
|
||||
await waitFor(() => expect(result.current).toBe('blob:mock-2'));
|
||||
|
||||
// SRC is still cached and resolves synchronously.
|
||||
expect(getMediaObjectUrl(SRC)).toBe('blob:mock-1');
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { ensureMediaObjectUrl, getMediaObjectUrl } from '$utils/mediaObjectUrlCache';
|
||||
|
||||
/**
|
||||
* Resolves `src` to a session-cached blob object URL on Tauri, where the media scheme
|
||||
* bypasses the webview's HTTP cache. Falls back to the raw URL on fetch failure.
|
||||
* Pass-through outside Tauri, where the service worker caches media.
|
||||
*/
|
||||
export const useTauriMediaObjectUrl = (src: string | undefined): string | undefined => {
|
||||
const active = isTauri() && src !== undefined;
|
||||
const [resolved, setResolved] = useState<string | undefined>(() =>
|
||||
active ? (getMediaObjectUrl(src) ?? undefined) : src
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) {
|
||||
setResolved(src);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
const cached = getMediaObjectUrl(src);
|
||||
if (cached !== undefined) {
|
||||
setResolved(cached);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
setResolved(undefined);
|
||||
ensureMediaObjectUrl(src).then(
|
||||
(objectUrl) => {
|
||||
if (!cancelled) setResolved(objectUrl);
|
||||
},
|
||||
() => {
|
||||
if (!cancelled) setResolved(src);
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [active, src]);
|
||||
|
||||
return active ? resolved : src;
|
||||
};
|
||||
@@ -1,5 +1,3 @@
|
||||
import { clearMediaObjectUrls } from './mediaObjectUrlCache';
|
||||
|
||||
const CACHE_NAME = 'sable-media-v2';
|
||||
const LEGACY_CACHE_NAMES = ['sable-media-v1'];
|
||||
const MAX_ENTRIES = 500;
|
||||
@@ -13,7 +11,6 @@ let legacyCachesCleaned = false;
|
||||
|
||||
export async function clearMediaCache(): Promise<void> {
|
||||
pendingBlobs.clear();
|
||||
clearMediaObjectUrls();
|
||||
writesSinceEviction = 0;
|
||||
if (typeof caches === 'undefined') return;
|
||||
try {
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { stubObjectUrls } from '../../test/objectUrlStub';
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({
|
||||
isTauri: () => false,
|
||||
}));
|
||||
|
||||
import {
|
||||
clearMediaObjectUrls,
|
||||
ensureMediaObjectUrl,
|
||||
getMediaObjectUrl,
|
||||
} from './mediaObjectUrlCache';
|
||||
|
||||
beforeEach(() => {
|
||||
stubObjectUrls();
|
||||
clearMediaObjectUrls();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('mediaObjectUrlCache', () => {
|
||||
it('holds picker loads to the shared media fetch budget', async () => {
|
||||
let inFlight = 0;
|
||||
let peak = 0;
|
||||
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(() => {
|
||||
inFlight += 1;
|
||||
peak = Math.max(peak, inFlight);
|
||||
return new Promise<Response>((resolve) => {
|
||||
setTimeout(() => {
|
||||
inFlight -= 1;
|
||||
resolve(new Response('image-bytes', { status: 200 }));
|
||||
}, 5);
|
||||
});
|
||||
});
|
||||
|
||||
const resolved = await Promise.all(
|
||||
Array.from({ length: 10 }, (_unused, index) =>
|
||||
ensureMediaObjectUrl(`https://example.org/media/${index}`)
|
||||
)
|
||||
);
|
||||
|
||||
expect(resolved).toHaveLength(10);
|
||||
expect(peak).toBe(3);
|
||||
});
|
||||
|
||||
it('revokes and forgets every entry on clear', async () => {
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('image-bytes', { status: 200 }));
|
||||
|
||||
await ensureMediaObjectUrl('https://example.org/media/a');
|
||||
expect(getMediaObjectUrl('https://example.org/media/a')).toBe('blob:mock-1');
|
||||
|
||||
clearMediaObjectUrls();
|
||||
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-1');
|
||||
expect(getMediaObjectUrl('https://example.org/media/a')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,59 +0,0 @@
|
||||
import { fetch } from '$utils/fetch';
|
||||
import { withMediaFetchSlot } from './mediaConcurrency';
|
||||
|
||||
// Tauri webviews don't HTTP-cache responses from the sable-media scheme handler, so
|
||||
// every remount round-trips into the native layer. Session object URLs avoid that.
|
||||
const MAX_ENTRIES = 256;
|
||||
|
||||
type CacheEntry = string | Promise<string>;
|
||||
|
||||
const objectUrls = new Map<string, CacheEntry>();
|
||||
|
||||
function remember(url: string, objectUrl: string): void {
|
||||
objectUrls.delete(url);
|
||||
objectUrls.set(url, objectUrl);
|
||||
while (objectUrls.size > MAX_ENTRIES) {
|
||||
const oldestKey = objectUrls.keys().next().value;
|
||||
if (oldestKey === undefined) break;
|
||||
const oldest = objectUrls.get(oldestKey);
|
||||
objectUrls.delete(oldestKey);
|
||||
if (typeof oldest === 'string') URL.revokeObjectURL(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
export function getMediaObjectUrl(url: string): string | undefined {
|
||||
const entry = objectUrls.get(url);
|
||||
if (typeof entry !== 'string') return undefined;
|
||||
remember(url, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function ensureMediaObjectUrl(url: string): Promise<string> {
|
||||
const existing = objectUrls.get(url);
|
||||
if (existing !== undefined) return Promise.resolve(existing);
|
||||
|
||||
const request = withMediaFetchSlot(async () => {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`media fetch failed: ${response.status}`);
|
||||
return response.blob();
|
||||
})
|
||||
.then((blob) => {
|
||||
const objectUrl = URL.createObjectURL(blob);
|
||||
remember(url, objectUrl);
|
||||
return objectUrl;
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (objectUrls.get(url) === request) objectUrls.delete(url);
|
||||
throw err;
|
||||
});
|
||||
|
||||
objectUrls.set(url, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
export function clearMediaObjectUrls(): void {
|
||||
objectUrls.forEach((entry) => {
|
||||
if (typeof entry === 'string') URL.revokeObjectURL(entry);
|
||||
});
|
||||
objectUrls.clear();
|
||||
}
|
||||
@@ -78,24 +78,6 @@ const getTauriMediaInnerTarget = (mediaUrl: string): string | undefined => {
|
||||
return mediaUrl;
|
||||
};
|
||||
|
||||
const MEDIA_THUMBNAIL_PATH_PREFIXES = [
|
||||
'/_matrix/client/v1/media/thumbnail/',
|
||||
'/_matrix/media/v3/thumbnail/',
|
||||
'/_matrix/media/r0/thumbnail/',
|
||||
];
|
||||
|
||||
// Matches on the inner http(s) target so tauri-wrapped and plain URLs classify alike.
|
||||
export const isThumbnailMediaUrl = (mediaUrl: string): boolean => {
|
||||
const innerTarget = getTauriMediaInnerTarget(mediaUrl);
|
||||
if (!innerTarget) return false;
|
||||
try {
|
||||
const { pathname } = new URL(innerTarget);
|
||||
return MEDIA_THUMBNAIL_PATH_PREFIXES.some((prefix) => pathname.startsWith(prefix));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Embeds a retry revision as a fragment on the inner http(s) target (stripping the
|
||||
// outer cache/session markers, which the rewrite re-adds). The fragment makes Rust's
|
||||
// cache_key(scope, target) distinct but is stripped natively before the upstream
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { vi } from 'vitest';
|
||||
|
||||
// jsdom has no URL.createObjectURL. Subclassing keeps `new URL()` working for code under test.
|
||||
export const stubObjectUrls = (): void => {
|
||||
let counter = 0;
|
||||
class StubbedURL extends globalThis.URL {
|
||||
static createObjectURL = vi.fn<() => string>(() => {
|
||||
counter += 1;
|
||||
return `blob:mock-${counter}`;
|
||||
});
|
||||
|
||||
static revokeObjectURL = vi.fn<() => void>();
|
||||
}
|
||||
|
||||
vi.stubGlobal('URL', StubbedURL);
|
||||
};
|
||||
Reference in new issue
Block a user