mirror of https://github.com/vector-im/riot-web
fix Content Messages upload abort
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>pull/21833/head
parent
37d04d6ceb
commit
50b8445d4d
|
@ -258,10 +258,10 @@ function readFileAsArrayBuffer(file) {
|
||||||
* If the file is encrypted then the object will have a "file" key.
|
* If the file is encrypted then the object will have a "file" key.
|
||||||
*/
|
*/
|
||||||
function uploadFile(matrixClient, roomId, file, progressHandler) {
|
function uploadFile(matrixClient, roomId, file, progressHandler) {
|
||||||
|
let canceled = false;
|
||||||
if (matrixClient.isRoomEncrypted(roomId)) {
|
if (matrixClient.isRoomEncrypted(roomId)) {
|
||||||
// If the room is encrypted then encrypt the file before uploading it.
|
// If the room is encrypted then encrypt the file before uploading it.
|
||||||
// First read the file into memory.
|
// First read the file into memory.
|
||||||
let canceled = false;
|
|
||||||
let uploadPromise;
|
let uploadPromise;
|
||||||
let encryptInfo;
|
let encryptInfo;
|
||||||
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
||||||
|
@ -278,9 +278,9 @@ function uploadFile(matrixClient, roomId, file, progressHandler) {
|
||||||
progressHandler: progressHandler,
|
progressHandler: progressHandler,
|
||||||
includeFilename: false,
|
includeFilename: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
return uploadPromise;
|
return uploadPromise;
|
||||||
}).then(function(url) {
|
}).then(function(url) {
|
||||||
|
if (canceled) throw new UploadCanceledError();
|
||||||
// If the attachment is encrypted then bundle the URL along
|
// If the attachment is encrypted then bundle the URL along
|
||||||
// with the information needed to decrypt the attachment and
|
// with the information needed to decrypt the attachment and
|
||||||
// add it under a file key.
|
// add it under a file key.
|
||||||
|
@ -300,11 +300,14 @@ function uploadFile(matrixClient, roomId, file, progressHandler) {
|
||||||
progressHandler: progressHandler,
|
progressHandler: progressHandler,
|
||||||
});
|
});
|
||||||
const promise1 = basePromise.then(function(url) {
|
const promise1 = basePromise.then(function(url) {
|
||||||
|
if (canceled) throw new UploadCanceledError();
|
||||||
// If the attachment isn't encrypted then include the URL directly.
|
// If the attachment isn't encrypted then include the URL directly.
|
||||||
return {"url": url};
|
return {"url": url};
|
||||||
});
|
});
|
||||||
// XXX: copy over the abort method to the new promise
|
promise1.abort = () => {
|
||||||
promise1.abort = basePromise.abort;
|
canceled = true;
|
||||||
|
MatrixClientPeg.get().cancelUpload(basePromise);
|
||||||
|
};
|
||||||
return promise1;
|
return promise1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -489,11 +492,16 @@ export default class ContentMessages {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
prom.abort = () => {
|
||||||
|
upload.cancelled = true;
|
||||||
|
};
|
||||||
|
|
||||||
const upload = {
|
const upload = {
|
||||||
fileName: file.name || 'Attachment',
|
fileName: file.name || 'Attachment',
|
||||||
roomId: roomId,
|
roomId: roomId,
|
||||||
total: 0,
|
total: 0,
|
||||||
loaded: 0,
|
loaded: 0,
|
||||||
|
promise: prom,
|
||||||
};
|
};
|
||||||
this.inprogress.push(upload);
|
this.inprogress.push(upload);
|
||||||
dis.dispatch({action: 'upload_started'});
|
dis.dispatch({action: 'upload_started'});
|
||||||
|
@ -510,6 +518,7 @@ export default class ContentMessages {
|
||||||
}
|
}
|
||||||
|
|
||||||
return prom.then(function() {
|
return prom.then(function() {
|
||||||
|
if (upload.canceled) throw new UploadCanceledError();
|
||||||
// XXX: upload.promise must be the promise that
|
// XXX: upload.promise must be the promise that
|
||||||
// is returned by uploadFile as it has an abort()
|
// is returned by uploadFile as it has an abort()
|
||||||
// method hacked onto it.
|
// method hacked onto it.
|
||||||
|
@ -524,6 +533,7 @@ export default class ContentMessages {
|
||||||
// Await previous message being sent into the room
|
// Await previous message being sent into the room
|
||||||
return promBefore;
|
return promBefore;
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
|
if (upload.canceled) throw new UploadCanceledError();
|
||||||
return matrixClient.sendMessage(roomId, content);
|
return matrixClient.sendMessage(roomId, content);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
error = err;
|
error = err;
|
||||||
|
|
Loading…
Reference in New Issue