From 52f644ae892756a9a884ce101a646c3465681cc1 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Mon, 14 Oct 2019 15:15:02 +0200 Subject: [PATCH] Upload files using SDK instead of XHR Use owncloud-sdk for uploading files for both the regular file view and also public page. Removed FileUpload.js as it is now obsolete. Had to switch to using paths instead of URLs in the upload handlers. There were some challenges because the public link page currently has the token embedded into the absolute path. Introduced dependency on join-path to avoid more path gymnastics in various places. --- apps/files/src/FileUpload.js | 61 ------------------- apps/files/src/components/FileDrop.vue | 4 +- apps/files/src/components/FileUpload.vue | 2 +- apps/files/src/components/FilesAppBar.vue | 39 +++++++----- apps/files/src/components/FolderUpload.vue | 2 +- .../src/components/PublicLinks/FilesDrop.vue | 54 ++++++++-------- apps/files/src/default.js | 2 +- apps/files/src/mixins.js | 47 ++++++++++---- package.json | 1 + yarn.lock | 28 ++++++++- 10 files changed, 118 insertions(+), 122 deletions(-) delete mode 100644 apps/files/src/FileUpload.js diff --git a/apps/files/src/FileUpload.js b/apps/files/src/FileUpload.js deleted file mode 100644 index 176fd533d8d..00000000000 --- a/apps/files/src/FileUpload.js +++ /dev/null @@ -1,61 +0,0 @@ -class FileUpload { - constructor (file, path, url, headers = {}, onProgress = () => {}, type = 'POST', onUploadEnded = () => {}) { - this.file = file - this.path = path - this.url = url - this.headers = headers - this.onProgress = onProgress - this.type = type - this.onUploadEnded = onUploadEnded - } - - upload (options = {}) { - const xhr = new XMLHttpRequest() - - // Headers - this.path = this.path.replace(/(^\/+)/, '') - let paths = this.path.split('/') - paths = paths.map(path => encodeURIComponent(path)) - this.path = paths.join('/') - xhr.open(this.type, this.url + this.path, true) - xhr.responseType = 'text' - if (options.overwrite) { - this.headers['If-Match'] = options.overwrite - delete this.headers['If-None-Match'] - } else { - this.headers['If-None-Match'] = '*' - delete this.headers['If-Match'] - } - this._setXhrHeaders(xhr) - - // Events - xhr.upload.addEventListener('progress', (e) => { - this.onProgress(e, this.file) - }, false) - - const promise = new Promise((resolve, reject) => { - xhr.onload = e => { - xhr.status >= 200 && xhr.status < 400 ? resolve(e) : reject(new Error(xhr.statusText)) - } - xhr.onloadend = e => { - this.onUploadEnded(this.file) - } - xhr.onerror = e => { - reject(e) - } - }) - - // Start upload - xhr.send(this.file) - - return promise - } - - _setXhrHeaders (xhr) { - Object.keys(this.headers).forEach(p => - xhr.setRequestHeader(p, this.headers[p]) - ) - } -} - -export default FileUpload diff --git a/apps/files/src/components/FileDrop.vue b/apps/files/src/components/FileDrop.vue index 7e15f6cddcb..549a50c2b9e 100644 --- a/apps/files/src/components/FileDrop.vue +++ b/apps/files/src/components/FileDrop.vue @@ -30,7 +30,7 @@ export default { }, props: { rootPath: { type: String, required: true }, - url: { type: String, required: true }, + path: { type: String, required: true }, headers: { type: Object, default: () => { @@ -45,7 +45,7 @@ export default { data () { return { ocDropzone_options: { - url: this.url, + url: '#', // FIXME: unused clickable: false, autoQueue: false } diff --git a/apps/files/src/components/FileUpload.vue b/apps/files/src/components/FileUpload.vue index df66c52b593..5e2bfc68d4e 100644 --- a/apps/files/src/components/FileUpload.vue +++ b/apps/files/src/components/FileUpload.vue @@ -12,7 +12,7 @@ import Mixins from '../mixins' export default { props: { - url: { type: String, required: true }, + path: { type: String, required: true }, headers: { type: Object, default: () => { diff --git a/apps/files/src/components/FilesAppBar.vue b/apps/files/src/components/FilesAppBar.vue index afc36a94ad1..e35906d0fa8 100644 --- a/apps/files/src/components/FilesAppBar.vue +++ b/apps/files/src/components/FilesAppBar.vue @@ -1,6 +1,6 @@