fix(tauri): render authenticated inline images via token blob-fetch when no service worker

This commit is contained in:
Erwan Leboucher committed 2026-07-17 14:17:03 +02:00
1 parent e786c20440
commit f8d7c050f5
2 files changed
+17 -4

No files matched your search

@@ -40,6 +40,8 @@ import { bytesToSize } from '$utils/common';
import { FALLBACK_MIMETYPE } from '$utils/mimeTypes';
import { stopPropagation } from '$utils/keyboard';
import { decryptFile, downloadEncryptedMedia, mxcUrlToHttp } from '$utils/matrix';
import { fetchMediaBlob } from '$utils/mediaTransport';
import { hasControllingServiceWorker } from '$utils/platform';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { ModalWide } from '$styles/Modal.css';
import { validBlurHash } from '$utils/blurHash';
@@ -100,6 +102,15 @@ export type ImageContentProps = {
containedStripMinPx?: number;
fillsPreviewSlot?: boolean;
};
// On web the service worker injects the media access token for <img src> URLs.
// Under Tauri there is no controlling service worker, so fetch the blob with the
// token (via mediaTransport) and render it as an object URL instead.
const toRenderableMediaUrl = async (rawUrl: string): Promise<string> => {
if (hasControllingServiceWorker()) return rawUrl;
const blob = await fetchMediaBlob(rawUrl);
return URL.createObjectURL(blob);
};
export const ImageContent = as<'div', ImageContentProps>(
(
{
@@ -158,7 +169,7 @@ export const ImageContent = as<'div', ImageContentProps>(
if (typeof matrixThumbnailMaxEdge === 'number' && matrixThumbnailMaxEdge > 0 && !encInfo) {
const { tw, th } = thumbnailDimsForMaxEdge(matrixThumbnailMaxEdge, info?.w, info?.h);
const thumbUrl = mxcUrlToHttp(mx, url, useAuthentication, tw, th, 'scale', false);
if (thumbUrl) return thumbUrl;
if (thumbUrl) return toRenderableMediaUrl(thumbUrl);
}
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
@@ -169,7 +180,7 @@ export const ImageContent = as<'div', ImageContentProps>(
);
return URL.createObjectURL(fileContent);
}
return mediaUrl;
return toRenderableMediaUrl(mediaUrl);
}, [mx, url, useAuthentication, mimeType, encInfo, matrixThumbnailMaxEdge, info?.w, info?.h])
);
@@ -190,7 +201,8 @@ export const ImageContent = as<'div', ImageContentProps>(
void (async () => {
const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication);
if (!mediaUrl || cancelled) return;
setViewerFullSrc(mediaUrl);
const renderable = await toRenderableMediaUrl(mediaUrl);
if (!cancelled) setViewerFullSrc(renderable);
})();
return () => {
cancelled = true;
+2 -1
View File
@@ -18,6 +18,7 @@ import './app/styles/themes.css';
import './app/styles/overrides/General.css';
import './app/styles/overrides/Privacy.css';
import './app/styles/overrides/TauriDesktop.css';
import { isTauri } from '@tauri-apps/api/core';
import { pushSessionToSW } from './sw-session';
import type { Sessions } from './app/state/sessions';
import { getFallbackSession, MATRIX_SESSIONS_KEY, ACTIVE_SESSION_KEY } from './app/state/sessions';
@@ -58,7 +59,7 @@ const sendSessionToSW = () => {
pushSessionToSW(active?.baseUrl, active?.accessToken, active?.userId);
};
if ('serviceWorker' in navigator) {
if ('serviceWorker' in navigator && !isTauri()) {
const isProduction = import.meta.env.MODE === 'production';
const swUrl = isProduction
? `${trimTrailingSlash(import.meta.env.BASE_URL)}/sw.js`