From c6e63d5724555acca95b3cef01625e6eb9f2ed69 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 29 Sep 2024 15:31:25 +0200 Subject: [PATCH] components/composer: add support for multiple file upload --- components/composer.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/components/composer.js b/components/composer.js index f199ff0..ad5452e 100644 --- a/components/composer.js +++ b/components/composer.js @@ -135,19 +135,9 @@ export default class Composer extends Component { return client && client.isupport.filehost() && !this.props.readOnly; } - async uploadFiles(fileList) { - if (fileList.length === 0 || !this.canUploadFiles()) { - return; - } - + async uploadFile(file) { let client = this.props.client; let endpoint = client.isupport.filehost(); - if (!endpoint) { - return; - } - - // TODO: support more than one file - let file = fileList.item(0); let auth; if (client.params.saslPlain) { @@ -186,13 +176,22 @@ export default class Composer extends Component { throw new Error("filehost response missing Location header field"); } - let uploadURL = new URL(loc, endpoint); + return new URL(loc, endpoint); + } + + async uploadFileList(fileList) { + let promises = []; + for (let file of fileList) { + promises.push(this.uploadFile(file)); + } + + let urls = await Promise.all(promises); this.setState((state) => { if (state.text) { - return { text: state.text + " " + uploadURL.toString() }; + return { text: state.text + " " + urls.join(" ") }; } else { - return { text: uploadURL.toString() }; + return { text: urls.join(" ") }; } }); } @@ -205,7 +204,7 @@ export default class Composer extends Component { event.preventDefault(); event.stopImmediatePropagation(); - await this.uploadFiles(event.clipboardData.files); + await this.uploadFileList(event.clipboardData.files); } handleDragOver(event) { @@ -230,7 +229,7 @@ export default class Composer extends Component { event.preventDefault(); event.stopImmediatePropagation(); - await this.uploadFiles(event.dataTransfer.files); + await this.uploadFileList(event.dataTransfer.files); } handleWindowKeyDown(event) {