File mode: support drag drop and fix cancellation of uploads

This commit is contained in:
N-Pex 2023-07-06 11:18:26 +02:00
parent fbacf68651
commit 2f29b72594
4 changed files with 125 additions and 67 deletions

View file

@ -30,27 +30,29 @@ var _browserCanRecordAudioF = function () {
}
var _browserCanRecordAudio = _browserCanRecordAudioF();
class UploadPromise extends Promise {
constructor(executor) {
const aborter = {
aborted: false,
abortablePromise: undefined,
matrixClient: undefined,
class UploadPromise {
aborted = false;
onAbort = undefined;
constructor(wrappedPromise) {
this.wrappedPromise = wrappedPromise;
}
abort() {
this.aborted = true;
if (this.onAbort) {
this.onAbort();
}
}
const normalExecutor = function (resolve, reject) {
executor(resolve, reject, aborter);
};
then(resolve, reject) {
this.wrappedPromise = this.wrappedPromise.then(resolve, reject);
return this;
}
super(normalExecutor);
this.abort = () => {
aborter.aborted = true;
if (aborter.abortablePromise && aborter.matrixClient) {
aborter.matrixClient.cancelUpload(aborter.abortablePromise);
aborter.matrixClient = undefined;
aborter.abortablePromise = undefined;
}
};
catch(handler) {
this.wrappedPromise = this.wrappedPromise.catch(handler);
return this;
}
}
@ -340,11 +342,11 @@ class Util {
}
sendImage(matrixClient, roomId, file, onUploadProgress, threadRoot) {
return new UploadPromise((resolve, reject, aborter) => {
const abortionController = aborter;
const uploadPromise = new UploadPromise(undefined);
uploadPromise.wrappedPromise = new Promise((resolve, reject) => {
var reader = new FileReader();
reader.onload = (e) => {
if (abortionController.aborted) {
if (uploadPromise.aborted) {
reject("Aborted");
return;
}
@ -399,9 +401,11 @@ class Util {
if (!matrixClient.isRoomEncrypted(roomId)) {
// Not encrypted.
abortionController.matrixClient = matrixClient;
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
abortionController.abortablePromise
const promise = matrixClient.uploadContent(data, opts);
uploadPromise.onAbort = () => {
matrixClient.cancelUpload(promise);
};
promise
.then((response) => {
messageContent.url = response.content_uri;
return this.sendMessage(matrixClient, roomId, "m.room.message", messageContent)
@ -448,9 +452,12 @@ class Util {
// Encrypted data sent as octet-stream!
opts.type = "application/octet-stream";
abortionController.abortablePromise = matrixClient.uploadContent(data, opts);
abortionController.abortablePromise
.then((response) => {
const promise = matrixClient.uploadContent(data, opts);
uploadPromise.onAbort = () => {
matrixClient.cancelUpload(promise);
};
promise
.then((response) => {
if (response.error) {
return reject(response.error);
}
@ -469,6 +476,7 @@ class Util {
}
reader.readAsArrayBuffer(file);
});
return uploadPromise;
}
/**