extract blob url code to platform (WIP)

This commit is contained in:
Bruno Windels 2020-10-26 10:14:46 +01:00
parent 4fd71279cf
commit a6224135e3

View File

@ -23,38 +23,38 @@ export class ImageTile extends MessageTile {
constructor(options) { constructor(options) {
super(options); super(options);
this._decryptedThumbailUrl = null; this._decryptedThumbail = null;
this._decryptedUrl = null; this._decryptedImage = null;
this.load(); this.load();
} }
async _loadEncryptedFile(file) { async _loadEncryptedFile(file) {
const buffer = await this._mediaRepository.downloadEncryptedFile(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) { if (this.isDisposed) {
return; return;
} }
return URL.createObjectURL(blob); // TODO: fix XSS bug here by not checking mimetype
// const blob = new Blob([buffer], {type: file.mimetype});
return this.track(this._platform.createBufferURI(buffer, file.mimetype));
} }
async load() { async load() {
const thumbnailFile = this._getContent().info?.thumbnail_file; const thumbnailFile = this._getContent().info?.thumbnail_file;
const file = this._getContent().file; const file = this._getContent().file;
if (thumbnailFile) { if (thumbnailFile) {
this._decryptedThumbailUrl = await this._loadEncryptedFile(thumbnailFile); this._decryptedThumbail = await this._loadEncryptedFile(thumbnailFile);
this.emitChange("thumbnailUrl"); this.emitChange("thumbnailUrl");
} else if (file) { } else if (file) {
this._decryptedUrl = await this._loadEncryptedFile(file); this._decryptedImage = await this._loadEncryptedFile(file);
this.emitChange("thumbnailUrl"); this.emitChange("thumbnailUrl");
} }
} }
get thumbnailUrl() { get thumbnailUrl() {
if (this._decryptedThumbailUrl) { if (this._decryptedThumbail) {
return this._decryptedThumbailUrl; return this._decryptedThumbail.uri;
} else if (this._decryptedUrl) { } else if (this._decryptedImage) {
return this._decryptedUrl; return this._decryptedImage.uri;
} }
const mxcUrl = this._getContent()?.url; const mxcUrl = this._getContent()?.url;
if (typeof mxcUrl === "string") { if (typeof mxcUrl === "string") {
@ -64,13 +64,13 @@ export class ImageTile extends MessageTile {
} }
async loadImageUrl() { async loadImageUrl() {
if (!this._decryptedUrl) { if (!this._decryptedImage) {
const file = this._getContent().file; const file = this._getContent().file;
if (file) { if (file) {
this._decryptedUrl = await this._loadEncryptedFile(file); this._decryptedImage = await this._loadEncryptedFile(file);
} }
} }
return this._decryptedUrl || ""; return this._decryptedImage || "";
} }
_scaleFactor() { _scaleFactor() {
@ -99,16 +99,4 @@ export class ImageTile extends MessageTile {
get shape() { get shape() {
return "image"; return "image";
} }
dispose() {
if (this._decryptedThumbailUrl) {
URL.revokeObjectURL(this._decryptedThumbailUrl);
this._decryptedThumbailUrl = null;
}
if (this._decryptedUrl) {
URL.revokeObjectURL(this._decryptedUrl);
this._decryptedUrl = null;
}
super.dispose();
}
} }