Skip to content

Commit

Permalink
Use multipart upload in the web ui
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Sep 14, 2021
1 parent 43f2858 commit 89e2e83
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
24 changes: 20 additions & 4 deletions apps/files/js/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,12 @@ OC.FileUpload.prototype = {
&& this.getFile().size > this.uploader.fileUploadParam.maxChunkSize
) {
data.isChunked = true;
var headers = (OC.getCapabilities().dav.chunking === '2.0') ? {
'X-Chunking-Destination': this.getTargetDestination()
} : {};

chunkFolderPromise = this.uploader.davClient.createDirectory(
'uploads/' + OC.getCurrentUser().uid + '/' + this.getId()
'uploads/' + OC.getCurrentUser().uid + '/' + this.getId(), headers
);
// TODO: if fails, it means same id already existed, need to retry
} else {
Expand Down Expand Up @@ -298,17 +302,24 @@ OC.FileUpload.prototype = {
}
if (size) {
headers['OC-Total-Length'] = size;

}
if (OC.getCapabilities().dav.chunking === '2.0') {
headers['X-Chunking-Destination'] = this.getTargetDestination();
}

return this.uploader.davClient.move(
'uploads/' + uid + '/' + this.getId() + '/.file',
'files/' + uid + '/' + OC.joinPaths(this.getFullPath(), this.getFileName()),
this.getTargetDestination(),
true,
headers
);
},

getTargetDestination: function() {
var uid = OC.getCurrentUser().uid;
return 'files/' + uid + '/' + OC.joinPaths(this.getFullPath(), this.getFileName())
},

_deleteChunkFolder: function() {
// delete transfer directory for this upload
this.uploader.davClient.remove(
Expand Down Expand Up @@ -1133,7 +1144,6 @@ OC.Uploader.prototype = _.extend({

if (options.maxChunkSize) {
this.fileUploadParam.maxChunkSize = options.maxChunkSize;
}

// initialize jquery fileupload (blueimp)
var fileupload = this.$uploadEl.fileupload(this.fileUploadParam);
Expand Down Expand Up @@ -1274,6 +1284,12 @@ OC.Uploader.prototype = _.extend({
var upload = self.getUpload(data);
var range = data.contentRange.split(' ')[1];
var chunkId = range.split('/')[0].split('-')[0];
if (OC.getCapabilities().dav.chunking === '2.0') {
// Calculate chunk index for usage with s3
chunkId = Math.ceil((data.chunkSize+Number(chunkId)) / upload.uploader.fileUploadParam.maxChunkSize);
data.headers['X-Chunking-Destination'] = upload.getTargetDestination();
}

data.url = OC.getRootPath() +
'/remote.php/dav/uploads' +
'/' + OC.getCurrentUser().uid +
Expand Down
8 changes: 7 additions & 1 deletion apps/files/js/jquery.fileupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@
promise = dfd.promise(),
jqXHR,
upload;

// Dynamically adjust the chunk size for Chunking V2 to fit into the 10000 chunk limit
if ((OC.getCapabilities().dav.chunking === '2.0') && file.size/mcs > 10000) {
mcs = Math.ceil(file.size/10000)
}

if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
options.data) {
return false;
Expand Down Expand Up @@ -1486,4 +1492,4 @@

});

}));
}));
9 changes: 5 additions & 4 deletions core/src/files/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ import escapeHTML from 'escape-html'
return promise
},

_simpleCall: function(method, path) {
_simpleCall: function(method, path, headers) {
if (!path) {
throw 'Missing argument "path"'
}
Expand All @@ -752,7 +752,8 @@ import escapeHTML from 'escape-html'

this._client.request(
method,
this._buildUrl(path)
this._buildUrl(path),
headers ? headers : {}
).then(
function(result) {
if (self._isSuccessStatus(result.status)) {
Expand All @@ -773,8 +774,8 @@ import escapeHTML from 'escape-html'
*
* @returns {Promise}
*/
createDirectory: function(path) {
return this._simpleCall('MKCOL', path)
createDirectory: function(path, headers) {
return this._simpleCall('MKCOL', path, headers)
},

/**
Expand Down

0 comments on commit 89e2e83

Please sign in to comment.