diff --git a/src/app/components/message/content/ImageContent.tsx b/src/app/components/message/content/ImageContent.tsx index fb2c288cf..116866dc0 100644 --- a/src/app/components/message/content/ImageContent.tsx +++ b/src/app/components/message/content/ImageContent.tsx @@ -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 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 => { + 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; diff --git a/src/index.tsx b/src/index.tsx index 14de49919..0319ac6e6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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`