2020-09-22 13:40:38 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-11-21 16:42:28 +01:00
|
|
|
import {encodeQueryParams} from "./common";
|
2020-10-23 17:18:11 +02:00
|
|
|
import {decryptAttachment} from "../e2ee/attachment.js";
|
2021-11-22 08:09:33 +01:00
|
|
|
import {Platform} from "../../platform/web/Platform.js";
|
|
|
|
import {BlobHandle} from "../../platform/web/dom/BlobHandle.js";
|
2021-12-01 07:12:24 +01:00
|
|
|
import type {Attachment, EncryptedFile} from "./types/response";
|
2020-09-22 13:40:38 +02:00
|
|
|
|
|
|
|
export class MediaRepository {
|
2021-11-22 08:09:33 +01:00
|
|
|
private readonly _homeserver: string;
|
|
|
|
private readonly _platform: Platform;
|
|
|
|
|
|
|
|
constructor({homeserver, platform}: {homeserver:string, platform: Platform}) {
|
2021-08-23 19:26:39 +02:00
|
|
|
this._homeserver = homeserver;
|
2020-10-30 15:18:27 +01:00
|
|
|
this._platform = platform;
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:21:19 +02:00
|
|
|
mxcUrlThumbnail(url: string, width: number, height: number, method: "crop" | "scale"): string | undefined {
|
2020-09-22 13:40:38 +02:00
|
|
|
const parts = this._parseMxcUrl(url);
|
|
|
|
if (parts) {
|
|
|
|
const [serverName, mediaId] = parts;
|
2021-08-23 19:26:39 +02:00
|
|
|
const httpUrl = `${this._homeserver}/_matrix/media/r0/thumbnail/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
2021-02-11 11:15:20 +01:00
|
|
|
return httpUrl + "?" + encodeQueryParams({width: Math.round(width), height: Math.round(height), method});
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
2022-04-26 14:21:19 +02:00
|
|
|
return undefined;
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:21:19 +02:00
|
|
|
mxcUrl(url: string): string | undefined {
|
2020-09-22 13:40:38 +02:00
|
|
|
const parts = this._parseMxcUrl(url);
|
|
|
|
if (parts) {
|
|
|
|
const [serverName, mediaId] = parts;
|
2021-08-23 19:26:39 +02:00
|
|
|
return `${this._homeserver}/_matrix/media/r0/download/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
2022-04-26 14:21:19 +02:00
|
|
|
return undefined;
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 14:21:19 +02:00
|
|
|
private _parseMxcUrl(url: string): string[] | undefined {
|
2020-09-22 13:40:38 +02:00
|
|
|
const prefix = "mxc://";
|
|
|
|
if (url.startsWith(prefix)) {
|
|
|
|
return url.substr(prefix.length).split("/", 2);
|
|
|
|
} else {
|
2022-04-26 14:21:19 +02:00
|
|
|
return undefined;
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 17:18:11 +02:00
|
|
|
|
2021-12-01 07:12:24 +01:00
|
|
|
async downloadEncryptedFile(fileEntry: EncryptedFile, cache: boolean = false): Promise<BlobHandle> {
|
2020-10-23 17:18:11 +02:00
|
|
|
const url = this.mxcUrl(fileEntry.url);
|
2020-11-10 17:49:48 +01:00
|
|
|
const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response();
|
2021-02-12 16:01:54 +01:00
|
|
|
const decryptedBuffer = await decryptAttachment(this._platform, encryptedBuffer, fileEntry);
|
2020-11-10 22:36:26 +01:00
|
|
|
return this._platform.createBlob(decryptedBuffer, fileEntry.mimetype);
|
2020-10-23 17:18:11 +02:00
|
|
|
}
|
2020-11-10 17:49:48 +01:00
|
|
|
|
2021-11-22 08:09:33 +01:00
|
|
|
async downloadPlaintextFile(mxcUrl: string, mimetype: string, cache: boolean = false): Promise<BlobHandle> {
|
2020-11-10 17:49:48 +01:00
|
|
|
const url = this.mxcUrl(mxcUrl);
|
|
|
|
const {body: buffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response();
|
2020-11-10 22:36:26 +01:00
|
|
|
return this._platform.createBlob(buffer, mimetype);
|
2020-11-10 17:49:48 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 07:12:24 +01:00
|
|
|
async downloadAttachment(content: Attachment, cache: boolean = false): Promise<BlobHandle> {
|
2020-11-10 17:49:48 +01:00
|
|
|
if (content.file) {
|
|
|
|
return this.downloadEncryptedFile(content.file, cache);
|
|
|
|
} else {
|
2021-11-22 08:09:33 +01:00
|
|
|
return this.downloadPlaintextFile(content.url!, content.info?.mimetype, cache);
|
2020-11-10 17:49:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 13:40:38 +02:00
|
|
|
}
|