This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrative example of MSC3916 using blobs/async auth
- Loading branch information
Showing
9 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2024 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React, {useEffect} from "react"; | ||
|
||
import {canAddAuthToMediaUrl, getMediaByUrl} from "../../../utils/media"; | ||
|
||
interface IProps extends React.HTMLProps<HTMLImageElement> { | ||
} | ||
|
||
const AuthedImage: React.FC<IProps> = (props) => { | ||
const [src, setSrc] = React.useState<string | undefined>(""); | ||
|
||
useEffect(() => { | ||
let blobUrl: string | undefined; | ||
async function getImage(): Promise<void> { | ||
if (props.src) { | ||
if (await canAddAuthToMediaUrl(props.src)) { | ||
const response = await getMediaByUrl(props.src); | ||
blobUrl = URL.createObjectURL(await response.blob()); | ||
setSrc(blobUrl); | ||
} else { | ||
// Skip blob caching if we're just doing a plain http(s) request. | ||
setSrc(props.src); | ||
} | ||
} | ||
} | ||
|
||
// noinspection JSIgnoredPromiseFromCall | ||
getImage(); | ||
|
||
return () => { | ||
// Cleanup | ||
if (blobUrl) { | ||
URL.revokeObjectURL(blobUrl); | ||
} | ||
}; | ||
}, [props.src]); | ||
|
||
const props2 = {...props}; | ||
props2.src = src; | ||
|
||
return ( | ||
// eslint-disable-next-line jsx-a11y/alt-text | ||
<img {...props2} /> | ||
); | ||
}; | ||
|
||
export default AuthedImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2024 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import {MatrixClientPeg} from "../MatrixClientPeg"; | ||
|
||
export async function canAddAuthToMediaUrl(url: string): Promise<boolean> { | ||
return url.includes("/_matrix/media/v3") && Boolean(await MatrixClientPeg.get()?.doesServerSupportUnstableFeature("org.matrix.msc3916")); | ||
} | ||
|
||
export async function getMediaByUrl(url: string): Promise<Response> { | ||
// If the server doesn't support unstable auth, don't use it :) | ||
if (!(await canAddAuthToMediaUrl(url))) { | ||
return fetch(url); | ||
} | ||
|
||
// We can rewrite the URL to support auth now, and request accordingly. | ||
url = url.replace(/\/media\/v3\/(.*)\//, "/client/unstable/org.matrix.msc3916/media/$1/"); | ||
return fetch(url, { | ||
headers: { | ||
'Authorization': `Bearer ${MatrixClientPeg.get()?.getAccessToken()}`, | ||
}, | ||
}); | ||
} |