2020-09-22 17:10:38 +05:30
|
|
|
/*
|
|
|
|
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";
|
2020-10-23 20:48:11 +05:30
|
|
|
import {decryptAttachment} from "../e2ee/attachment.js";
|
2020-09-22 17:10:38 +05:30
|
|
|
|
|
|
|
export class MediaRepository {
|
2020-10-30 19:48:27 +05:30
|
|
|
constructor({homeServer, platform}) {
|
2020-10-23 20:48:11 +05:30
|
|
|
this._homeServer = homeServer;
|
2020-10-30 19:48:27 +05:30
|
|
|
this._platform = platform;
|
2020-09-22 17:10:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
mxcUrlThumbnail(url, width, height, method) {
|
|
|
|
const parts = this._parseMxcUrl(url);
|
|
|
|
if (parts) {
|
|
|
|
const [serverName, mediaId] = parts;
|
2020-10-23 20:48:11 +05:30
|
|
|
const httpUrl = `${this._homeServer}/_matrix/media/r0/thumbnail/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
2020-09-22 17:10:38 +05:30
|
|
|
return httpUrl + "?" + encodeQueryParams({width, height, method});
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxcUrl(url) {
|
|
|
|
const parts = this._parseMxcUrl(url);
|
|
|
|
if (parts) {
|
|
|
|
const [serverName, mediaId] = parts;
|
2020-10-23 20:48:11 +05:30
|
|
|
return `${this._homeServer}/_matrix/media/r0/download/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
2020-09-22 17:10:38 +05:30
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_parseMxcUrl(url) {
|
|
|
|
const prefix = "mxc://";
|
|
|
|
if (url.startsWith(prefix)) {
|
|
|
|
return url.substr(prefix.length).split("/", 2);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 20:48:11 +05:30
|
|
|
|
2020-11-10 22:19:48 +05:30
|
|
|
async downloadEncryptedFile(fileEntry, cache = false) {
|
2020-10-23 20:48:11 +05:30
|
|
|
const url = this.mxcUrl(fileEntry.url);
|
2020-11-10 22:19:48 +05:30
|
|
|
const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response();
|
2020-10-30 19:48:27 +05:30
|
|
|
const decryptedBuffer = await decryptAttachment(this._platform.crypto, encryptedBuffer, fileEntry);
|
2020-11-11 03:06:26 +05:30
|
|
|
return this._platform.createBlob(decryptedBuffer, fileEntry.mimetype);
|
2020-10-23 20:48:11 +05:30
|
|
|
}
|
2020-11-10 22:19:48 +05:30
|
|
|
|
|
|
|
async downloadPlaintextFile(mxcUrl, mimetype, cache = false) {
|
|
|
|
const url = this.mxcUrl(mxcUrl);
|
|
|
|
const {body: buffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response();
|
2020-11-11 03:06:26 +05:30
|
|
|
return this._platform.createBlob(buffer, mimetype);
|
2020-11-10 22:19:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAttachment(content, cache = false) {
|
|
|
|
if (content.file) {
|
|
|
|
return this.downloadEncryptedFile(content.file, cache);
|
|
|
|
} else {
|
|
|
|
return this.downloadPlaintextFile(content.url, content.info?.mimetype, cache);
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 17:10:38 +05:30
|
|
|
}
|