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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
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;
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class ImageTile extends MessageTile {
|
2020-10-23 20:48:11 +05:30
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-10-26 14:19:42 +05:30
|
|
|
this._decryptedThumbailUrl = null;
|
2020-10-23 20:48:11 +05:30
|
|
|
this._decryptedUrl = null;
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:19:42 +05:30
|
|
|
async _loadEncryptedFile(file) {
|
|
|
|
const buffer = await this._mediaRepository.downloadEncryptedFile(file);
|
|
|
|
// TODO: fix XSS bug here by not checking mimetype
|
|
|
|
const blob = new Blob([buffer], {type: file.mimetype});
|
|
|
|
if (this.isDisposed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return URL.createObjectURL(blob);
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:48:11 +05:30
|
|
|
async load() {
|
2020-10-26 14:19:42 +05:30
|
|
|
const thumbnailFile = this._getContent().info?.thumbnail_file;
|
|
|
|
const file = this._getContent().file;
|
2020-10-23 20:48:11 +05:30
|
|
|
if (thumbnailFile) {
|
2020-10-26 14:19:42 +05:30
|
|
|
this._decryptedThumbailUrl = await this._loadEncryptedFile(thumbnailFile);
|
|
|
|
this.emitChange("thumbnailUrl");
|
|
|
|
} else if (file) {
|
|
|
|
this._decryptedUrl = await this._loadEncryptedFile(file);
|
2020-10-23 20:48:11 +05:30
|
|
|
this.emitChange("thumbnailUrl");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 23:32:08 +05:30
|
|
|
get thumbnailUrl() {
|
2020-10-26 14:19:42 +05:30
|
|
|
if (this._decryptedThumbailUrl) {
|
|
|
|
return this._decryptedThumbailUrl;
|
|
|
|
} else if (this._decryptedUrl) {
|
2020-10-23 20:48:11 +05:30
|
|
|
return this._decryptedUrl;
|
|
|
|
}
|
2020-08-19 15:06:43 +05:30
|
|
|
const mxcUrl = this._getContent()?.url;
|
|
|
|
if (typeof mxcUrl === "string") {
|
2020-08-20 19:10:43 +05:30
|
|
|
return this._mediaRepository.mxcUrlThumbnail(mxcUrl, this.thumbnailWidth, this.thumbnailHeight, "scale");
|
2020-08-17 13:59:29 +05:30
|
|
|
}
|
2020-10-23 20:48:11 +05:30
|
|
|
return "";
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|
2020-05-09 23:32:08 +05:30
|
|
|
|
2020-10-26 14:19:42 +05:30
|
|
|
async loadImageUrl() {
|
|
|
|
if (!this._decryptedUrl) {
|
|
|
|
const file = this._getContent().file;
|
|
|
|
if (file) {
|
|
|
|
this._decryptedUrl = await this._loadEncryptedFile(file);
|
|
|
|
}
|
2020-08-17 13:59:29 +05:30
|
|
|
}
|
2020-10-26 14:19:42 +05:30
|
|
|
return this._decryptedUrl || "";
|
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
|
|
|
}
|
2019-06-02 03:33:55 +05:30
|
|
|
|
|
|
|
get label() {
|
2020-05-09 23:32:08 +05:30
|
|
|
return this._getContent().body;
|
|
|
|
}
|
|
|
|
|
|
|
|
get shape() {
|
|
|
|
return "image";
|
2019-06-02 03:33:55 +05:30
|
|
|
}
|
2020-10-23 20:48:11 +05:30
|
|
|
|
|
|
|
dispose() {
|
2020-10-26 14:19:42 +05:30
|
|
|
if (this._decryptedThumbailUrl) {
|
|
|
|
URL.revokeObjectURL(this._decryptedThumbailUrl);
|
|
|
|
this._decryptedThumbailUrl = null;
|
|
|
|
}
|
2020-10-23 20:48:11 +05:30
|
|
|
if (this._decryptedUrl) {
|
|
|
|
URL.revokeObjectURL(this._decryptedUrl);
|
|
|
|
this._decryptedUrl = null;
|
|
|
|
}
|
|
|
|
super.dispose();
|
|
|
|
}
|
2019-03-09 05:10:03 +05:30
|
|
|
}
|