Skip to content

Commit b62f879

Browse files
authored
Merge pull request #30637 from nextcloud/backport/30620/stable22
[stable22] Properly abort uploads
2 parents bf234ca + 8a9fcb6 commit b62f879

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

apps/files/js/file-upload.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,26 +322,32 @@ OC.FileUpload.prototype = {
322322
);
323323
},
324324

325+
_delete: function() {
326+
if (this.data.isChunked) {
327+
this._deleteChunkFolder()
328+
}
329+
this.deleteUpload();
330+
},
331+
325332
/**
326333
* Abort the upload
327334
*/
328335
abort: function() {
329-
if (this.data.isChunked) {
330-
this._deleteChunkFolder();
336+
if (this.aborted) {
337+
return
331338
}
332-
this.data.abort();
333-
this.deleteUpload();
334339
this.aborted = true;
340+
this._delete();
335341
},
336342

337343
/**
338344
* Fail the upload
339345
*/
340346
fail: function() {
341-
this.deleteUpload();
342-
if (this.data.isChunked) {
343-
this._deleteChunkFolder();
347+
if (this.aborted) {
348+
return
344349
}
350+
this._delete();
345351
},
346352

347353
/**
@@ -679,7 +685,26 @@ OC.Uploader.prototype = _.extend({
679685
return;
680686
}
681687

682-
delete this._uploads[upload.data.uploadId];
688+
// defer as some calls/chunks might still be busy failing, so we need
689+
// the upload info there still
690+
var self = this;
691+
var uploadId = upload.data.uploadId;
692+
// mark as deleted for the progress bar
693+
this._uploads[uploadId].deleted = true;
694+
window.setTimeout(function() {
695+
delete self._uploads[uploadId];
696+
}, 5000)
697+
},
698+
699+
_activeUploadCount: function() {
700+
var count = 0;
701+
for (var key in this._uploads) {
702+
if (!this._uploads[key].deleted) {
703+
count++;
704+
}
705+
}
706+
707+
return count;
683708
},
684709

685710
showUploadCancelMessage: _.debounce(function() {
@@ -905,6 +930,7 @@ OC.Uploader.prototype = _.extend({
905930
if ($uploadEl.exists()) {
906931
this.progressBar.on('cancel', function() {
907932
self.cancelUploads();
933+
self.showUploadCancelMessage();
908934
});
909935

910936
this.fileUploadParam = {
@@ -1075,14 +1101,18 @@ OC.Uploader.prototype = _.extend({
10751101
var upload = self.getUpload(data);
10761102
var status = null;
10771103
if (upload) {
1104+
if (upload.aborted) {
1105+
// uploads might fail with errors from the server when aborted
1106+
return
1107+
}
10781108
status = upload.getResponseStatus();
10791109
}
10801110
self.log('fail', e, upload);
10811111

10821112
self.removeUpload(upload);
10831113

10841114
if (data.textStatus === 'abort' || data.errorThrown === 'abort') {
1085-
self.showUploadCancelMessage();
1115+
return
10861116
} else if (status === 412) {
10871117
// file already exists
10881118
self.showConflict(upload);
@@ -1283,6 +1313,10 @@ OC.Uploader.prototype = _.extend({
12831313
fileupload.on('fileuploadchunksend', function(e, data) {
12841314
// modify the request to adjust it to our own chunking
12851315
var upload = self.getUpload(data);
1316+
if (!upload) {
1317+
// likely cancelled
1318+
return
1319+
}
12861320
var range = data.contentRange.split(' ')[1];
12871321
var chunkId = range.split('/')[0].split('-')[0];
12881322
data.url = OC.getRootPath() +
@@ -1298,9 +1332,9 @@ OC.Uploader.prototype = _.extend({
12981332

12991333
self._pendingUploadDoneCount++;
13001334

1301-
upload.done().then(function() {
1335+
upload.done().always(function() {
13021336
self._pendingUploadDoneCount--;
1303-
if (Object.keys(self._uploads).length === 0 && self._pendingUploadDoneCount === 0) {
1337+
if (self._activeUploadCount() === 0 && self._pendingUploadDoneCount === 0) {
13041338
// All the uploads ended and there is no pending
13051339
// operation, so hide the progress bar.
13061340
// Note that this happens here only with chunked
@@ -1314,9 +1348,13 @@ OC.Uploader.prototype = _.extend({
13141348
// hides the progress bar in that case).
13151349
self._hideProgressBar();
13161350
}
1317-
1351+
}).done(function() {
13181352
self.trigger('done', e, upload);
13191353
}).fail(function(status, response) {
1354+
if (upload.aborted) {
1355+
return
1356+
}
1357+
13201358
var message = response.message;
13211359
if (status === 507) {
13221360
// not enough space

0 commit comments

Comments
 (0)