|
@@ -116,8 +116,6 @@ module.exports = class Tus extends Plugin {
|
|
|
* @returns {Promise}
|
|
|
*/
|
|
|
upload (file, current, total) {
|
|
|
- this.uppy.log(`uploading ${current} of ${total}`)
|
|
|
-
|
|
|
this.resetUploaderReferences(file.id)
|
|
|
|
|
|
// Create a new tus upload
|
|
@@ -208,93 +206,109 @@ module.exports = class Tus extends Plugin {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this.uppy.log(file.remote.url)
|
|
|
if (file.serverToken) {
|
|
|
- this.connectToServerSocket(file)
|
|
|
- } else {
|
|
|
- let endpoint = this.opts.endpoint
|
|
|
- if (file.tus && file.tus.endpoint) {
|
|
|
- endpoint = file.tus.endpoint
|
|
|
+ return this.connectToServerSocket(file)
|
|
|
+ .then(() => resolve())
|
|
|
+ .catch(reject)
|
|
|
+ }
|
|
|
+
|
|
|
+ let endpoint = this.opts.endpoint
|
|
|
+ if (file.tus && file.tus.endpoint) {
|
|
|
+ endpoint = file.tus.endpoint
|
|
|
+ }
|
|
|
+
|
|
|
+ this.uppy.emit('upload-started', file.id)
|
|
|
+
|
|
|
+ fetch(file.remote.url, {
|
|
|
+ method: 'post',
|
|
|
+ credentials: 'include',
|
|
|
+ headers: {
|
|
|
+ 'Accept': 'application/json',
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ },
|
|
|
+ body: JSON.stringify(Object.assign({}, file.remote.body, {
|
|
|
+ endpoint,
|
|
|
+ protocol: 'tus',
|
|
|
+ size: file.data.size,
|
|
|
+ metadata: file.meta
|
|
|
+ }))
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ if (res.status < 200 || res.status > 300) {
|
|
|
+ return reject(res.statusText)
|
|
|
}
|
|
|
|
|
|
- this.uppy.emit('upload-started', file.id)
|
|
|
-
|
|
|
- fetch(file.remote.url, {
|
|
|
- method: 'post',
|
|
|
- credentials: 'include',
|
|
|
- headers: {
|
|
|
- 'Accept': 'application/json',
|
|
|
- 'Content-Type': 'application/json'
|
|
|
- },
|
|
|
- body: JSON.stringify(Object.assign({}, file.remote.body, {
|
|
|
- endpoint,
|
|
|
- protocol: 'tus',
|
|
|
- size: file.data.size,
|
|
|
- metadata: file.meta
|
|
|
- }))
|
|
|
- })
|
|
|
- .then((res) => {
|
|
|
- if (res.status < 200 || res.status > 300) {
|
|
|
- return reject(res.statusText)
|
|
|
- }
|
|
|
-
|
|
|
- res.json().then((data) => {
|
|
|
- const token = data.token
|
|
|
- this.uppy.setFileState(file.id, { serverToken: token })
|
|
|
- file = this.getFile(file.id)
|
|
|
- this.connectToServerSocket(file)
|
|
|
- resolve()
|
|
|
- })
|
|
|
+ return res.json().then((data) => {
|
|
|
+ this.uppy.setFileState(file.id, { serverToken: data.token })
|
|
|
+ file = this.getFile(file.id)
|
|
|
+ return file
|
|
|
})
|
|
|
- }
|
|
|
+ })
|
|
|
+ .then((file) => {
|
|
|
+ return this.connectToServerSocket(file)
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ reject(new Error(err))
|
|
|
+ })
|
|
|
})
|
|
|
}
|
|
|
|
|
|
connectToServerSocket (file) {
|
|
|
- const token = file.serverToken
|
|
|
- const host = getSocketHost(file.remote.host)
|
|
|
- const socket = new UppySocket({ target: `${host}/api/${token}` })
|
|
|
- this.uploaderSockets[file.id] = socket
|
|
|
- this.uploaderEvents[file.id] = createEventTracker(this.uppy)
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const token = file.serverToken
|
|
|
+ const host = getSocketHost(file.remote.host)
|
|
|
+ const socket = new UppySocket({ target: `${host}/api/${token}` })
|
|
|
+ this.uploaderSockets[file.id] = socket
|
|
|
+ this.uploaderEvents[file.id] = createEventTracker(this.uppy)
|
|
|
|
|
|
- this.onFileRemove(file.id, () => socket.send('pause', {}))
|
|
|
+ this.onFileRemove(file.id, () => {
|
|
|
+ socket.send('pause', {})
|
|
|
+ resolve(`upload ${file.id} was removed`)
|
|
|
+ })
|
|
|
|
|
|
- this.onPause(file.id, (isPaused) => {
|
|
|
- isPaused ? socket.send('pause', {}) : socket.send('resume', {})
|
|
|
- })
|
|
|
+ this.onPause(file.id, (isPaused) => {
|
|
|
+ isPaused ? socket.send('pause', {}) : socket.send('resume', {})
|
|
|
+ })
|
|
|
|
|
|
- this.onPauseAll(file.id, () => socket.send('pause', {}))
|
|
|
+ this.onPauseAll(file.id, () => socket.send('pause', {}))
|
|
|
|
|
|
- this.onCancelAll(file.id, () => socket.send('pause', {}))
|
|
|
+ this.onCancelAll(file.id, () => socket.send('pause', {}))
|
|
|
|
|
|
- this.onResumeAll(file.id, () => {
|
|
|
- if (file.error) {
|
|
|
- socket.send('pause', {})
|
|
|
- }
|
|
|
- socket.send('resume', {})
|
|
|
- })
|
|
|
+ this.onResumeAll(file.id, () => {
|
|
|
+ if (file.error) {
|
|
|
+ socket.send('pause', {})
|
|
|
+ }
|
|
|
+ socket.send('resume', {})
|
|
|
+ })
|
|
|
|
|
|
- this.onRetry(file.id, () => {
|
|
|
- socket.send('pause', {})
|
|
|
- socket.send('resume', {})
|
|
|
- })
|
|
|
+ this.onRetry(file.id, () => {
|
|
|
+ socket.send('pause', {})
|
|
|
+ socket.send('resume', {})
|
|
|
+ })
|
|
|
|
|
|
- this.onRetryAll(file.id, () => {
|
|
|
- socket.send('pause', {})
|
|
|
- socket.send('resume', {})
|
|
|
- })
|
|
|
+ this.onRetryAll(file.id, () => {
|
|
|
+ socket.send('pause', {})
|
|
|
+ socket.send('resume', {})
|
|
|
+ })
|
|
|
|
|
|
- if (file.isPaused) {
|
|
|
- socket.send('pause', {})
|
|
|
- }
|
|
|
+ if (file.isPaused) {
|
|
|
+ socket.send('pause', {})
|
|
|
+ }
|
|
|
|
|
|
- socket.on('progress', (progressData) => emitSocketProgress(this, progressData, file))
|
|
|
+ socket.on('progress', (progressData) => emitSocketProgress(this, progressData, file))
|
|
|
|
|
|
- socket.on('error', (errData) => {
|
|
|
- this.uppy.emit('core:upload-error', file.id, new Error(errData.error))
|
|
|
- })
|
|
|
+ socket.on('error', (errData) => {
|
|
|
+ this.uppy.emit('upload-error', file.id, new Error(errData.error))
|
|
|
+ reject(new Error(errData.error))
|
|
|
+ })
|
|
|
|
|
|
- socket.on('success', (data) => {
|
|
|
- this.uppy.emit('upload-success', file.id, data, data.url)
|
|
|
- this.resetUploaderReferences(file.id)
|
|
|
+ socket.on('success', (data) => {
|
|
|
+ this.uppy.emit('upload-success', file.id, data, data.url)
|
|
|
+ this.resetUploaderReferences(file.id)
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
})
|
|
|
}
|
|
|
|
|
@@ -381,10 +395,14 @@ module.exports = class Tus extends Plugin {
|
|
|
|
|
|
if (file.error) {
|
|
|
return Promise.reject(new Error(file.error))
|
|
|
- } else if (!file.isRemote) {
|
|
|
- return this.upload(file, current, total)
|
|
|
- } else {
|
|
|
+ }
|
|
|
+
|
|
|
+ this.uppy.log(`uploading ${current} of ${total}`)
|
|
|
+
|
|
|
+ if (file.isRemote) {
|
|
|
return this.uploadRemote(file, current, total)
|
|
|
+ } else {
|
|
|
+ return this.upload(file, current, total)
|
|
|
}
|
|
|
})
|
|
|
|