Skip to content

Commit

Permalink
fix: Fix video and audio loading with authenicated media (#1946)
Browse files Browse the repository at this point in the history
Appeareantly Firefox (and maybe Chrome) won't let service workers take over requests from <video> and <audio> tags, so we just fetch the URL ourselves.
  • Loading branch information
ShadowRZ authored Sep 11, 2024
1 parent 5482f8e commit f2c31d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/app/components/message/content/AudioContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function AudioContent({

const [srcState, loadSrc] = useAsyncCallback(
useCallback(
() => getFileSrcUrl(mxcUrlToHttp(mx, url, useAuthentication) ?? '', mimeType, encInfo),
() => getFileSrcUrl(mxcUrlToHttp(mx, url, useAuthentication) ?? '', mimeType, encInfo, true),
[mx, url, useAuthentication, mimeType, encInfo]
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/message/content/VideoContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const VideoContent = as<'div', VideoContentProps>(

const [srcState, loadSrc] = useAsyncCallback(
useCallback(
() => getFileSrcUrl(mxcUrlToHttp(mx, url, useAuthentication) ?? '', mimeType, encInfo),
() => getFileSrcUrl(mxcUrlToHttp(mx, url, useAuthentication) ?? '', mimeType, encInfo, true),
[mx, url, useAuthentication, mimeType, encInfo]
)
);
Expand Down
9 changes: 8 additions & 1 deletion src/app/components/message/content/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { decryptFile } from '../../../utils/matrix';
export const getFileSrcUrl = async (
httpUrl: string,
mimeType: string,
encInfo?: EncryptedAttachmentInfo
encInfo?: EncryptedAttachmentInfo,
forceFetch?: boolean
): Promise<string> => {
if (encInfo) {
if (typeof httpUrl !== 'string') throw new Error('Malformed event');
Expand All @@ -13,6 +14,12 @@ export const getFileSrcUrl = async (
const decryptedBlob = await decryptFile(encData, mimeType, encInfo);
return URL.createObjectURL(decryptedBlob);
}
if (forceFetch) {
const res = await fetch(httpUrl, { method: 'GET' });
const blob = await res.blob();
return URL.createObjectURL(blob);
}

return httpUrl;
};

Expand Down

0 comments on commit f2c31d2

Please sign in to comment.