aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/js/file-upload.js
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/js/file-upload.js')
-rw-r--r--apps/files/js/file-upload.js98
1 files changed, 84 insertions, 14 deletions
diff --git a/apps/files/js/file-upload.js b/apps/files/js/file-upload.js
index b86b42bdb9a..d1730fa7bc7 100644
--- a/apps/files/js/file-upload.js
+++ b/apps/files/js/file-upload.js
@@ -220,8 +220,8 @@ OC.FileUpload.prototype = {
this.data.headers['If-None-Match'] = '*';
}
- 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'] =
@@ -234,7 +234,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
@@ -260,9 +260,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())
);
},
@@ -271,12 +280,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();
+ }
},
/**
@@ -286,7 +303,23 @@ OC.FileUpload.prototype = {
*/
getResponse: function() {
var response = this.data.response();
- if (typeof response.result !== 'string') {
+ if (response.errorThrown) {
+ // attempt parsing Sabre exception is available
+ var xml = response.jqXHR.responseXML;
+ if (xml.documentElement.localName === 'error' && xml.documentElement.namespaceURI === 'DAV:') {
+ var messages = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'message');
+ var exceptions = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'exception');
+ if (messages.length) {
+ response.message = messages[0].textContent;
+ }
+ if (exceptions.length) {
+ response.exception = exceptions[0].textContent;
+ }
+ return response;
+ }
+ }
+
+ if (typeof response.result !== 'string' && response.result) {
//fetch response from iframe
response = $.parseJSON(response.result[0].body.innerText);
if (!response) {
@@ -376,6 +409,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
* also see article @link http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata
@@ -721,6 +761,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()
+ });
$uploadEl = $($uploadEl);
this.$uploadEl = $uploadEl;
@@ -900,6 +947,7 @@ OC.Uploader.prototype = _.extend({
status = upload.getResponseStatus();
}
self.log('fail', e, upload);
+ self._hideProgressBar();
if (data.textStatus === 'abort') {
self.showUploadCancelMessage();
@@ -916,11 +964,16 @@ OC.Uploader.prototype = _.extend({
self.cancelUploads();
} else {
// HTTP connection problem or other error
- OC.Notification.show(data.errorThrown, {type: 'error'});
+ var message = '';
+ if (upload) {
+ var response = upload.getResponse();
+ message = response.message;
+ }
+ OC.Notification.show(message || data.errorThrown, {type: 'error'});
}
if (upload) {
- upload.deleteUpload();
+ upload.fail();
}
},
/**
@@ -951,6 +1004,10 @@ OC.Uploader.prototype = _.extend({
}
};
+ if (options.maxChunkSize) {
+ this.fileUploadParam.maxChunkSize = options.maxChunkSize;
+ }
+
// initialize jquery fileupload (blueimp)
var fileupload = this.$uploadEl.fileupload(this.fileUploadParam);
@@ -1041,7 +1098,6 @@ OC.Uploader.prototype = _.extend({
self.log('progress handle fileuploadstop', e, data);
self.clear();
- self._hideProgressBar();
self.trigger('stop', e, data);
});
fileupload.on('fileuploadfail', function(e, data) {
@@ -1096,7 +1152,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) +
@@ -1108,7 +1164,21 @@ OC.Uploader.prototype = _.extend({
fileupload.on('fileuploaddone', function(e, data) {
var upload = self.getUpload(data);
upload.done().then(function() {
+ self._hideProgressBar();
self.trigger('done', e, upload);
+ }).fail(function(status, response) {
+ var message = response.message;
+ self._hideProgressBar();
+ if (status === 507) {
+ // not enough space
+ OC.Notification.show(message || t('files', 'Not enough free space'), {type: 'error'});
+ self.cancelUploads();
+ } else if (status === 409) {
+ OC.Notification.show(message || t('files', 'Target folder does not exist any more'), {type: 'error'});
+ } else {
+ OC.Notification.show(message || t('files', 'Error when assembling chunks, status code {status}', {status: status}), {type: 'error'});
+ }
+ self.trigger('fail', e, data);
});
});
fileupload.on('fileuploaddrop', function(e, data) {