Skip to content

Commit

Permalink
Fix folder upload race condition
Browse files Browse the repository at this point in the history
When uploading the same folder twice, sometimes a race condition is
triggered where some of the first uploaded items finish faster and
trigger the "end of transfer" event even though there are further items
to upload.

This fix prevents the end of transfer event to clear the list of uploads
so it can continue.
  • Loading branch information
Vincent Petry committed Nov 3, 2017
1 parent 0d2c70a commit 89a761f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
16 changes: 11 additions & 5 deletions apps/files/js/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ OC.FileUpload = function(uploader, data) {
if (!data) {
throw 'Missing "data" argument in OC.FileUpload constructor';
}
var path = '';
var basePath = '';
if (this.uploader.fileList) {
path = OC.joinPaths(this.uploader.fileList.getCurrentDirectory(), this.getFile().name);
} else {
path = this.getFile().name;
basePath = this.uploader.fileList.getCurrentDirectory();
}
var path = OC.joinPaths(basePath, this.getFile().relativePath || '', this.getFile().name);
this.id = 'web-file-upload-' + md5(path) + '-' + (new Date()).getTime();
};
OC.FileUpload.CONFLICT_MODE_DETECT = 0;
Expand Down Expand Up @@ -622,7 +621,13 @@ OC.Uploader.prototype = _.extend({
* Clear uploads
*/
clear: function() {
this._uploads = {};
var remainingUploads = {};
_.each(this._uploads, function(upload, key) {
if (!upload.isDone) {
remainingUploads[key] = upload;
}
});
this._uploads = remainingUploads;
this._knownDirs = {};
},
/**
Expand Down Expand Up @@ -1092,6 +1097,7 @@ OC.Uploader.prototype = _.extend({
var upload = self.getUpload(data);
var that = $(this);
self.log('done', e, upload);
upload.isDone = true;

var status = upload.getResponseStatus();
if (status < 200 || status >= 300) {
Expand Down
18 changes: 0 additions & 18 deletions apps/files/js/upload.js

This file was deleted.

10 changes: 10 additions & 0 deletions apps/files/tests/js/fileUploadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ describe('OC.Upload tests', function() {
expect(upload).toBeDefined();
expect(upload.getFileName()).toEqual('test.txt');
});
it('clear leaves pending uploads', function() {
uploader._uploads = {
'abc': {name: 'a job well done.txt', isDone: true},
'def': {name: 'whatevs.txt'}
};

uploader.clear();

expect(uploader._uploads).toEqual({'def': {name: 'whatevs.txt'}});
});
});
describe('Upload conflicts', function() {
var conflictDialogStub;
Expand Down

0 comments on commit 89a761f

Please sign in to comment.