This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/domain/session/room/timeline/tiles/ImageTile.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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 {MessageTile} from "./MessageTile.js";
2019-03-09 05:10:03 +05:30
2020-05-09 23:32:08 +05:30
const MAX_HEIGHT = 300;
const MAX_WIDTH = 400;
export class ImageTile extends MessageTile {
constructor(options) {
super(options);
this._decryptedUrl = null;
this.load();
}
async load() {
const thumbnailFile = this._getContent().file;
if (thumbnailFile) {
const buffer = await this._mediaRepository.downloadEncryptedFile(thumbnailFile);
// TODO: fix XSS bug here by not checking mimetype
const blob = new Blob([buffer], {type: thumbnailFile.mimetype});
if (this.isDisposed) {
return;
}
this._decryptedUrl = URL.createObjectURL(blob);
this.emitChange("thumbnailUrl");
}
}
2020-05-09 23:32:08 +05:30
get thumbnailUrl() {
if (this._decryptedUrl) {
return this._decryptedUrl;
}
2020-08-19 15:06:43 +05:30
const mxcUrl = this._getContent()?.url;
if (typeof mxcUrl === "string") {
return this._mediaRepository.mxcUrlThumbnail(mxcUrl, this.thumbnailWidth, this.thumbnailHeight, "scale");
2020-08-17 13:59:29 +05:30
}
return "";
2019-03-09 05:10:03 +05:30
}
2020-05-09 23:32:08 +05:30
get url() {
if (this._decryptedUrl) {
return this._decryptedUrl;
}
2020-08-19 15:06:43 +05:30
const mxcUrl = this._getContent()?.url;
if (typeof mxcUrl === "string") {
return this._mediaRepository.mxcUrl(mxcUrl);
2020-08-17 13:59:29 +05:30
}
return "";
2019-03-09 05:10:03 +05:30
}
2020-05-09 23:32:08 +05:30
_scaleFactor() {
2020-08-19 15:06:43 +05:30
const info = this._getContent()?.info;
const scaleHeightFactor = MAX_HEIGHT / info?.h;
const scaleWidthFactor = MAX_WIDTH / info?.w;
2020-05-09 23:32:08 +05:30
// take the smallest scale factor, to respect all constraints
// we should not upscale images, so limit scale factor to 1 upwards
return Math.min(scaleWidthFactor, scaleHeightFactor, 1);
2019-03-09 05:10:03 +05:30
}
2020-05-09 23:32:08 +05:30
get thumbnailWidth() {
2020-08-19 15:06:43 +05:30
const info = this._getContent()?.info;
return Math.round(info?.w * this._scaleFactor());
2020-05-09 23:32:08 +05:30
}
2020-05-10 00:51:55 +05:30
get thumbnailHeight() {
2020-08-19 15:06:43 +05:30
const info = this._getContent()?.info;
return Math.round(info?.h * this._scaleFactor());
2019-03-09 05:10:03 +05:30
}
get label() {
2020-05-09 23:32:08 +05:30
return this._getContent().body;
}
get shape() {
return "image";
}
dispose() {
if (this._decryptedUrl) {
URL.revokeObjectURL(this._decryptedUrl);
this._decryptedUrl = null;
}
super.dispose();
}
2019-03-09 05:10:03 +05:30
}