Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use chunking in web upload #26306

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/files/appinfo/app.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

use Symfony\Component\EventDispatcher\GenericEvent;
/**
* @author Jakob Sack <mail@jakobsack.de>
* @author Joas Schilling <coding@schilljs.com>
Expand Down Expand Up @@ -58,3 +60,17 @@
\OC::$server->getConfig()
);
});

$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener(
'OCP\Config::js',
function($event) {
if ($event instanceof GenericEvent) {
$maxChunkSize = (int)(\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', (10 * 1024 * 1024)));
$event->setArgument('files', [
'max_chunk_size' => $maxChunkSize
]);
}
}
);

3 changes: 2 additions & 1 deletion apps/files/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
direction: $('#defaultFileSortingDirection').val()
},
config: this._filesConfig,
enableUpload: true
enableUpload: true,
maxChunkSize: OC.appConfig.files.max_chunk_size
}
);
this.files.initialize();
Expand Down
57 changes: 46 additions & 11 deletions apps/files/js/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ OC.FileUpload.prototype = {
this.data.headers['X-OC-Mtime'] = file.lastModified / 1000;
}

var userName = this.uploader.filesClient.getUserName();
var password = this.uploader.filesClient.getPassword();
var userName = this.uploader.davClient.getUserName();
var password = this.uploader.davClient.getPassword();
if (userName) {
// copy username/password from DAV client
this.data.headers['Authorization'] =
Expand All @@ -258,7 +258,7 @@ OC.FileUpload.prototype = {
&& this.getFile().size > this.uploader.fileUploadParam.maxChunkSize
) {
data.isChunked = true;
chunkFolderPromise = this.uploader.filesClient.createDirectory(
chunkFolderPromise = this.uploader.davClient.createDirectory(
'uploads/' + encodeURIComponent(OC.getCurrentUser().uid) + '/' + encodeURIComponent(this.getId())
);
// TODO: if fails, it means same id already existed, need to retry
Expand All @@ -284,9 +284,18 @@ OC.FileUpload.prototype = {
}

var uid = OC.getCurrentUser().uid;
return this.uploader.filesClient.move(
return this.uploader.davClient.move(
'uploads/' + encodeURIComponent(uid) + '/' + encodeURIComponent(this.getId()) + '/.file',
'files/' + encodeURIComponent(uid) + '/' + OC.joinPaths(this.getFullPath(), this.getFileName())
'files/' + encodeURIComponent(uid) + '/' + OC.joinPaths(this.getFullPath(), this.getFileName()),
true,
{'X-OC-Mtime': this.getFile().lastModified / 1000}
);
},

_deleteChunkFolder: function() {
// delete transfer directory for this upload
this.uploader.davClient.remove(
'uploads/' + encodeURIComponent(OC.getCurrentUser().uid) + '/' + encodeURIComponent(this.getId())
);
},

Expand All @@ -295,12 +304,20 @@ OC.FileUpload.prototype = {
*/
abort: function() {
if (this.data.isChunked) {
// delete transfer directory for this upload
this.uploader.filesClient.remove(
'uploads/' + encodeURIComponent(OC.getCurrentUser().uid) + '/' + encodeURIComponent(this.getId())
);
this._deleteChunkFolder();
}
this.data.abort();
this.deleteUpload();
},

/**
* Fail the upload
*/
fail: function() {
this.deleteUpload();
if (this.data.isChunked) {
this._deleteChunkFolder();
}
},

/**
Expand Down Expand Up @@ -399,6 +416,13 @@ OC.Uploader.prototype = _.extend({
*/
filesClient: null,

/**
* Webdav client pointing at the root "dav" endpoint
*
* @type OC.Files.Client
*/
davClient: null,

/**
* Function that will allow us to know if Ajax uploads are supported
* @link https://github.com/New-Bamboo/example-ajax-upload/blob/master/public/index.html
Expand Down Expand Up @@ -758,6 +782,13 @@ OC.Uploader.prototype = _.extend({

this.fileList = options.fileList;
this.filesClient = options.filesClient || OC.Files.getClient();
this.davClient = new OC.Files.Client({
host: this.filesClient.getHost(),
root: OC.linkToRemoteBase('dav'),
useHTTPS: OC.getProtocol() === 'https',
userName: this.filesClient.getUserName(),
password: this.filesClient.getPassword()
});

if (options.url) {
this.url = options.url;
Expand Down Expand Up @@ -970,7 +1001,7 @@ OC.Uploader.prototype = _.extend({
}

if (upload) {
upload.deleteUpload();
upload.fail();
}
},
/**
Expand Down Expand Up @@ -1001,6 +1032,10 @@ 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 @@ -1091,7 +1126,7 @@ OC.Uploader.prototype = _.extend({
// modify the request to adjust it to our own chunking
var upload = self.getUpload(data);
var range = data.contentRange.split(' ')[1];
var chunkId = range.split('/')[0];
var chunkId = range.split('/')[0].split('-')[0];
data.url = OC.getRootPath() +
'/remote.php/dav/uploads' +
'/' + encodeURIComponent(OC.getCurrentUser().uid) +
Expand Down
3 changes: 2 additions & 1 deletion apps/files/js/filelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@
this._uploader = new OC.Uploader($uploadEl, {
fileList: this,
filesClient: this.filesClient,
dropZone: $('#content')
dropZone: $('#content'),
maxChunkSize: options.maxChunkSize
});

this.setupUploadEvents(this._uploader);
Expand Down
14 changes: 11 additions & 3 deletions core/js/config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Björn Schießle <bjoern@schiessle.org>
Expand Down Expand Up @@ -37,6 +38,8 @@
*
*/

use Symfony\Component\EventDispatcher\GenericEvent;

// Set the content type to Javascript
header("Content-type: text/javascript");

Expand Down Expand Up @@ -198,12 +201,17 @@
$array['oc_defaults']['docBaseUrl'] = $defaults->getDocBaseUrl();
$array['oc_defaults']['docPlaceholderUrl'] = $defaults->buildDocLinkToKey('PLACEHOLDER');
}
$array['oc_appconfig'] = json_encode($array['oc_appconfig']);
$array['oc_config'] = json_encode($array['oc_config']);
$array['oc_defaults'] = json_encode($array['oc_defaults']);

// Allow hooks to modify the output values
OC_Hook::emit('\OCP\Config', 'js', ['array' => &$array]);
$event = \OC::$server->getEventDispatcher()->dispatch('OCP\Config::js', new GenericEvent());
foreach ($event->getArguments() as $key => $value) {
$array['oc_appconfig'][$key] = $value;
}

$array['oc_appconfig'] = json_encode($array['oc_appconfig']);
$array['oc_config'] = json_encode($array['oc_config']);
$array['oc_defaults'] = json_encode($array['oc_defaults']);

// Echo it
foreach ($array as $setting => $value) {
Expand Down
19 changes: 15 additions & 4 deletions core/js/files/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
}

url += options.host + this._root;
this._host = options.host;
this._defaultHeaders = options.defaultHeaders || {
'X-Requested-With': 'XMLHttpRequest',
'requesttoken': OC.requestToken
Expand Down Expand Up @@ -676,10 +677,11 @@
* @param {String} destinationPath destination path
* @param {boolean} [allowOverwrite=false] true to allow overwriting,
* false otherwise
* @param {Object} [headers=null] additional headers
*
* @return {Promise} promise
*/
move: function(path, destinationPath, allowOverwrite) {
move: function(path, destinationPath, allowOverwrite, headers) {
if (!path) {
throw 'Missing argument "path"';
}
Expand All @@ -690,9 +692,9 @@
var self = this;
var deferred = $.Deferred();
var promise = deferred.promise();
var headers = {
headers = _.extend({}, headers, {
'Destination' : this._buildUrl(destinationPath)
};
});

if (!allowOverwrite) {
headers['Overwrite'] = 'F';
Expand Down Expand Up @@ -761,6 +763,16 @@
*/
getBaseUrl: function() {
return this._client.baseUrl;
},

/**
* Returns the host
*
* @since 9.2
* @return {String} base URL
*/
getHost: function() {
return this._host;
}
};

Expand Down Expand Up @@ -798,7 +810,6 @@

var client = new OC.Files.Client({
host: OC.getHost(),
port: OC.getPort(),
root: OC.linkToRemoteBase('webdav'),
useHTTPS: OC.getProtocol() === 'https'
});
Expand Down