mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-24 12:04:57 +01:00
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
|
/*
|
||
|
Copyright 2020 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 {encodeQueryParams} from "./common.js";
|
||
|
|
||
|
export class MediaRepository {
|
||
|
constructor(homeserver) {
|
||
|
this._homeserver = homeserver;
|
||
|
}
|
||
|
|
||
|
mxcUrlThumbnail(url, width, height, method) {
|
||
|
const parts = this._parseMxcUrl(url);
|
||
|
if (parts) {
|
||
|
const [serverName, mediaId] = parts;
|
||
|
const httpUrl = `${this._homeserver}/_matrix/media/r0/thumbnail/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
||
|
return httpUrl + "?" + encodeQueryParams({width, height, method});
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
mxcUrl(url) {
|
||
|
const parts = this._parseMxcUrl(url);
|
||
|
if (parts) {
|
||
|
const [serverName, mediaId] = parts;
|
||
|
return `${this._homeserver}/_matrix/media/r0/download/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_parseMxcUrl(url) {
|
||
|
const prefix = "mxc://";
|
||
|
if (url.startsWith(prefix)) {
|
||
|
return url.substr(prefix.length).split("/", 2);
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
}
|