From 7d81306a497e172b2f3d6b1a2e596c3b2ba38e2f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 30 Oct 2020 15:18:27 +0100 Subject: [PATCH] return BufferHandles from the media repository BufferHandles are platform specific handles to a buffer. On web, they have a .blob and .url property. --- .../session/room/timeline/tiles/ImageTile.js | 5 +++-- src/matrix/SessionContainer.js | 3 +-- src/matrix/net/MediaRepository.js | 11 +++++------ src/platform/web/Platform.js | 6 +++--- .../web/dom/{BufferURL.js => BufferHandle.js} | 19 ++++++++++++++----- 5 files changed, 26 insertions(+), 18 deletions(-) rename src/platform/web/dom/{BufferURL.js => BufferHandle.js} (89%) diff --git a/src/domain/session/room/timeline/tiles/ImageTile.js b/src/domain/session/room/timeline/tiles/ImageTile.js index f4212912..1e31e414 100644 --- a/src/domain/session/room/timeline/tiles/ImageTile.js +++ b/src/domain/session/room/timeline/tiles/ImageTile.js @@ -35,11 +35,12 @@ export class ImageTile extends MessageTile { } async _loadEncryptedFile(file) { - const buffer = await this._mediaRepository.downloadEncryptedFile(file); + const bufferHandle = await this._mediaRepository.downloadEncryptedFile(file); if (this.isDisposed) { + bufferHandle.dispose(); return; } - return this.track(this.platform.createBufferURL(buffer, file.mimetype)); + return this.track(bufferHandle); } async load() { diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index e0fcf951..4f28c946 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -167,8 +167,7 @@ export class SessionContainer { this._requestScheduler.start(); const mediaRepository = new MediaRepository({ homeServer: sessionInfo.homeServer, - crypto: this._platform.crypto, - request: this._platform.request, + platform: this._platform, }); this._session = new Session({ storage: this._storage, diff --git a/src/matrix/net/MediaRepository.js b/src/matrix/net/MediaRepository.js index 856c6657..a20b6d1c 100644 --- a/src/matrix/net/MediaRepository.js +++ b/src/matrix/net/MediaRepository.js @@ -18,10 +18,9 @@ import {encodeQueryParams} from "./common.js"; import {decryptAttachment} from "../e2ee/attachment.js"; export class MediaRepository { - constructor({homeServer, crypto, request}) { + constructor({homeServer, platform}) { this._homeServer = homeServer; - this._crypto = crypto; - this._request = request; + this._platform = platform; } mxcUrlThumbnail(url, width, height, method) { @@ -55,8 +54,8 @@ export class MediaRepository { async downloadEncryptedFile(fileEntry) { const url = this.mxcUrl(fileEntry.url); - const {body: encryptedBuffer} = await this._request(url, {method: "GET", format: "buffer", cache: true}).response(); - const decryptedBuffer = await decryptAttachment(this._crypto, encryptedBuffer, fileEntry); - return decryptedBuffer; + const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache: true}).response(); + const decryptedBuffer = await decryptAttachment(this._platform.crypto, encryptedBuffer, fileEntry); + return this._platform.createBufferHandle(decryptedBuffer, fileEntry.mimetype); } } diff --git a/src/platform/web/Platform.js b/src/platform/web/Platform.js index dc299ef8..fa3451db 100644 --- a/src/platform/web/Platform.js +++ b/src/platform/web/Platform.js @@ -27,7 +27,7 @@ import {OnlineStatus} from "./dom/OnlineStatus.js"; import {Crypto} from "./dom/Crypto.js"; import {estimateStorageUsage} from "./dom/StorageEstimate.js"; import {WorkerPool} from "./dom/WorkerPool.js"; -import {BufferURL} from "./dom/BufferURL.js"; +import {BufferHandle} from "./dom/BufferHandle.js"; function addScript(src) { return new Promise(function (resolve, reject) { @@ -129,7 +129,7 @@ export class Platform { this._serviceWorkerHandler?.setNavigation(navigation); } - createBufferURL(buffer, mimetype) { - return new BufferURL(buffer, mimetype); + createBufferHandle(buffer, mimetype) { + return new BufferHandle(buffer, mimetype); } } diff --git a/src/platform/web/dom/BufferURL.js b/src/platform/web/dom/BufferHandle.js similarity index 89% rename from src/platform/web/dom/BufferURL.js rename to src/platform/web/dom/BufferHandle.js index 28730022..80bb40bb 100644 --- a/src/platform/web/dom/BufferURL.js +++ b/src/platform/web/dom/BufferHandle.js @@ -69,18 +69,27 @@ const ALLOWED_BLOB_MIMETYPES = { 'audio/x-flac': true, }; -export class BufferURL { +export class BufferHandle { constructor(buffer, mimetype) { mimetype = mimetype ? mimetype.split(";")[0].trim() : ''; if (!ALLOWED_BLOB_MIMETYPES[mimetype]) { mimetype = 'application/octet-stream'; } - const blob = new Blob([buffer], {type: mimetype}); - this.url = URL.createObjectURL(blob); + this.blob = new Blob([buffer], {type: mimetype}); + this._url = null; + } + + get url() { + if (!this._url) { + this._url = URL.createObjectURL(this.blob); + } + return this._url; } dispose() { - URL.revokeObjectURL(this.url); - this.url = null; + if (this._url) { + URL.revokeObjectURL(this._url); + this._url = null; + } } }