components/composer: add support for multiple file upload

This commit is contained in:
Simon Ser 2024-09-29 15:31:25 +02:00
parent 2f6efb56de
commit c6e63d5724

View file

@ -135,19 +135,9 @@ export default class Composer extends Component {
return client && client.isupport.filehost() && !this.props.readOnly; return client && client.isupport.filehost() && !this.props.readOnly;
} }
async uploadFiles(fileList) { async uploadFile(file) {
if (fileList.length === 0 || !this.canUploadFiles()) {
return;
}
let client = this.props.client; let client = this.props.client;
let endpoint = client.isupport.filehost(); let endpoint = client.isupport.filehost();
if (!endpoint) {
return;
}
// TODO: support more than one file
let file = fileList.item(0);
let auth; let auth;
if (client.params.saslPlain) { if (client.params.saslPlain) {
@ -186,13 +176,22 @@ export default class Composer extends Component {
throw new Error("filehost response missing Location header field"); 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) => { this.setState((state) => {
if (state.text) { if (state.text) {
return { text: state.text + " " + uploadURL.toString() }; return { text: state.text + " " + urls.join(" ") };
} else { } else {
return { text: uploadURL.toString() }; return { text: urls.join(" ") };
} }
}); });
} }
@ -205,7 +204,7 @@ export default class Composer extends Component {
event.preventDefault(); event.preventDefault();
event.stopImmediatePropagation(); event.stopImmediatePropagation();
await this.uploadFiles(event.clipboardData.files); await this.uploadFileList(event.clipboardData.files);
} }
handleDragOver(event) { handleDragOver(event) {
@ -230,7 +229,7 @@ export default class Composer extends Component {
event.preventDefault(); event.preventDefault();
event.stopImmediatePropagation(); event.stopImmediatePropagation();
await this.uploadFiles(event.dataTransfer.files); await this.uploadFileList(event.dataTransfer.files);
} }
handleWindowKeyDown(event) { handleWindowKeyDown(event) {