From 373a42c7a86f32c4d02e79d4df80382b387abf4d Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 10 Nov 2020 17:49:48 +0100 Subject: [PATCH] allow downloading plaintext attachments also, with or without cache --- .../session/room/timeline/tiles/ImageTile.js | 2 +- src/matrix/net/MediaRepository.js | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/domain/session/room/timeline/tiles/ImageTile.js b/src/domain/session/room/timeline/tiles/ImageTile.js index 1e31e414..71b5b9d6 100644 --- a/src/domain/session/room/timeline/tiles/ImageTile.js +++ b/src/domain/session/room/timeline/tiles/ImageTile.js @@ -35,7 +35,7 @@ export class ImageTile extends MessageTile { } async _loadEncryptedFile(file) { - const bufferHandle = await this._mediaRepository.downloadEncryptedFile(file); + const bufferHandle = await this._mediaRepository.downloadEncryptedFile(file, true); if (this.isDisposed) { bufferHandle.dispose(); return; diff --git a/src/matrix/net/MediaRepository.js b/src/matrix/net/MediaRepository.js index a20b6d1c..f04c387f 100644 --- a/src/matrix/net/MediaRepository.js +++ b/src/matrix/net/MediaRepository.js @@ -52,10 +52,25 @@ export class MediaRepository { } } - async downloadEncryptedFile(fileEntry) { + async downloadEncryptedFile(fileEntry, cache = false) { const url = this.mxcUrl(fileEntry.url); - const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache: true}).response(); + const {body: encryptedBuffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response(); const decryptedBuffer = await decryptAttachment(this._platform.crypto, encryptedBuffer, fileEntry); return this._platform.createBufferHandle(decryptedBuffer, fileEntry.mimetype); } + + async downloadPlaintextFile(mxcUrl, mimetype, cache = false) { + const url = this.mxcUrl(mxcUrl); + const {body: buffer} = await this._platform.request(url, {method: "GET", format: "buffer", cache}).response(); + return this._platform.createBufferHandle(buffer, mimetype); + } + + async downloadAttachment(content, cache = false) { + if (content.file) { + return this.downloadEncryptedFile(content.file, cache); + } else { + return this.downloadPlaintextFile(content.url, content.info?.mimetype, cache); + } + } + }