From a3aa25449ba506015314ec9cb2b8a3646e1e77ed Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 26 Oct 2020 17:37:32 +0100 Subject: [PATCH] make it work with xhr --- src/matrix/net/MediaRepository.js | 2 +- src/platform/web/dom/request/xhr.js | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/matrix/net/MediaRepository.js b/src/matrix/net/MediaRepository.js index ac953448..856c6657 100644 --- a/src/matrix/net/MediaRepository.js +++ b/src/matrix/net/MediaRepository.js @@ -55,7 +55,7 @@ export class MediaRepository { async downloadEncryptedFile(fileEntry) { const url = this.mxcUrl(fileEntry.url); - const {body: encryptedBuffer} = await this._request(url, {format: "buffer", cache: true}).response(); + const {body: encryptedBuffer} = await this._request(url, {method: "GET", format: "buffer", cache: true}).response(); const decryptedBuffer = await decryptAttachment(this._crypto, encryptedBuffer, fileEntry); return decryptedBuffer; } diff --git a/src/platform/web/dom/request/xhr.js b/src/platform/web/dom/request/xhr.js index 38a189fd..e397c336 100644 --- a/src/platform/web/dom/request/xhr.js +++ b/src/platform/web/dom/request/xhr.js @@ -35,19 +35,22 @@ class RequestResult { } } -function send(url, options) { +function send(url, {method, headers, timeout, body, format}) { const xhr = new XMLHttpRequest(); - xhr.open(options.method, url); - if (options.headers) { - for(const [name, value] of options.headers.entries()) { + if (format === "buffer") { + xhr.responseType = "arraybuffer"; + } + xhr.open(method, url); + if (headers) { + for(const [name, value] of headers.entries()) { xhr.setRequestHeader(name, value); } } - if (options.timeout) { - xhr.timeout = options.timeout; + if (timeout) { + xhr.timeout = timeout; } - xhr.send(options.body || null); + xhr.send(body || null); return xhr; } @@ -62,12 +65,17 @@ function xhrAsPromise(xhr, method, url) { } export function xhrRequest(url, options) { - url = addCacheBuster(url); + const {cache, format} = options; + if (!cache) { + url = addCacheBuster(url); + } const xhr = send(url, options); const promise = xhrAsPromise(xhr, options.method, url).then(xhr => { const {status} = xhr; let body = null; - if (xhr.getResponseHeader("Content-Type") === "application/json") { + if (format === "buffer") { + body = xhr.response; + } else if (xhr.getResponseHeader("Content-Type") === "application/json") { body = JSON.parse(xhr.responseText); } return {status, body};