From 8ea530e70a1928a72bf11c2e9f3a0af7ea3c0e1f Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Sat, 5 Jan 2019 09:02:39 +0100 Subject: [PATCH 01/11] chunked upload callbacks freature --- src/chunk/ChunkUploadHandler.js | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/chunk/ChunkUploadHandler.js b/src/chunk/ChunkUploadHandler.js index 97f21de..7701b44 100644 --- a/src/chunk/ChunkUploadHandler.js +++ b/src/chunk/ChunkUploadHandler.js @@ -16,6 +16,38 @@ export default class ChunkUploadHandler { this.options = options } + get startPhaseSuccessResponseCallback () { + let callbacks = this.options.chunkedUploadCallbacks || null + if (callbacks && !this.options.callbacks.startPhaseSuccessResponse instanceof Function) { + throw new TypeError('Function type is required in "startPhaseSuccessResponse" callback', "ChunkUploadHandler", 22); + } + return this.options.callbacks.startPhaseSuccessResponse + } + + get uploadPhaseSuccessResponseCallback () { + let callbacks = this.options.chunkedUploadCallbacks || null + if (callbacks && !this.options.callbacks.uploadPhaseSuccessResponse instanceof Function) { + throw new TypeError('Function type is required in "uploadPhaseSuccessResponse" callback', "ChunkUploadHandler", 31); + } + return this.options.callbacks.uploadPhaseSuccessResponse + } + + get finishPhaseBeforeRequestCallback () { + let callbacks = this.options.chunkedUploadCallbacks || null + if (callbacks && !this.options.callbacks.finishPhaseBeforeRequest instanceof Function) { + throw new TypeError('Function type is required in "finishPhaseBeforeRequest" callback', "ChunkUploadHandler", 39); + } + return this.options.callbacks.finishPhaseBeforeRequest + } + + get finishPhaseSuccessResponseCallback () { + let callbacks = this.options.chunkedUploadCallbacks || null + if (callbacks && !this.options.callbacks.finishPhaseSuccessResponse instanceof Function) { + throw new TypeError('Function type is required in "finishPhaseSuccessResponse" callback', "ChunkUploadHandler", 31); + } + return this.options.callbacks.finishPhaseSuccessResponse + } + /** * Gets the max retries from options */ @@ -241,6 +273,12 @@ export default class ChunkUploadHandler { this.sessionId = res.data.session_id this.chunkSize = res.data.end_offset + if (this.startPhaseSuccessResponseCallback) { + this.startPhaseSuccessResponseCallback( + this.sessionId + ) + } + this.createChunks() this.startChunking() }).catch(res => { @@ -308,6 +346,12 @@ export default class ChunkUploadHandler { })).then(res => { chunk.active = false if (res.status === 'success') { + if (this.uploadPhaseSuccessResponseCallback) { + this.uploadPhaseSuccessResponseCallback( + this.sessionId, + res.data !== null ? res.data : null + ) + } chunk.uploaded = true } else { if (chunk.retries-- <= 0) { @@ -335,6 +379,13 @@ export default class ChunkUploadHandler { finish () { this.updateFileProgress() + if (this.finishPhaseBeforeRequestCallback) { + this.finishBody = this.finishPhaseBeforeRequestCallback( + this.sessionId, + this.finishBody + ) + } + request({ method: 'POST', headers: Object.assign({}, this.headers, { @@ -351,6 +402,11 @@ export default class ChunkUploadHandler { return this.reject('server') } + if (this.finishPhaseSuccessResponseCallback) { + this.finishBody = this.finishPhaseSuccessResponseCallback( + this.sessionId + ) + } this.resolve(res) }).catch(res => { this.file.response = res @@ -358,3 +414,4 @@ export default class ChunkUploadHandler { }) } } + From db757a3c18a4caf92435fadb5ee4defdc2592290 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Sat, 5 Jan 2019 14:05:56 +0100 Subject: [PATCH 02/11] chunked upload feature update after tests + adding documentaion --- docs/docs/en.md | 95 +++++++++++++++++++++++++ src/FileUpload.vue | 16 ++++- src/chunk/ChunkUploadHandler.js | 118 +++++++++++++++++--------------- 3 files changed, 172 insertions(+), 57 deletions(-) diff --git a/docs/docs/en.md b/docs/docs/en.md index 04615c2..9c8fa62 100644 --- a/docs/docs/en.md +++ b/docs/docs/en.md @@ -209,6 +209,62 @@ Use the `handler` parameter to use a different Handler } ``` +##### Extending the handler using callbacks properties + +Another option for extending handler is possible with below dedicated peperties: + * chunk-enabled - must be `true` + * chunk-callbacks-enabled - must be `true` + * chunk-callbacks - nust be `Object` with interface based on `DefaultChunkUploadCallbacks` + +###### How to use calbacks in Vue template and script + +```html + + +