Skip to content

Commit

Permalink
make sure that we reset serverToken when an upload fails (#4376)
Browse files Browse the repository at this point in the history
also: sync duplicated code between all plugins

fixes #4356
  • Loading branch information
mifi authored Mar 29, 2023
1 parent 2d1135e commit 643fd8d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
9 changes: 7 additions & 2 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ export default class AwsS3Multipart extends BasePlugin {
return res.token
}

// NOTE! Keep this duplicated code in sync with other plugins
// TODO we should probably abstract this into a common function
async uploadRemote (file) {
this.resetUploaderReferences(file.id)

Expand All @@ -605,13 +607,16 @@ export default class AwsS3Multipart extends BasePlugin {

try {
if (file.serverToken) {
return this.connectToServerSocket(file)
return await this.connectToServerSocket(file)
}
const serverToken = await this.#queueRequestSocketToken(file)

if (!this.uppy.getState().files[file.id]) return undefined

this.uppy.setFileState(file.id, { serverToken })
return this.connectToServerSocket(this.uppy.getFile(file.id))
return await this.connectToServerSocket(this.uppy.getFile(file.id))
} catch (err) {
this.uppy.setFileState(file.id, { serverToken: undefined })
this.uppy.emit('upload-error', file, err)
throw err
}
Expand Down
9 changes: 6 additions & 3 deletions packages/@uppy/aws-s3/src/MiniXHRUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,28 @@ export default class MiniXHRUpload {
return res.token
}

// NOTE! Keep this duplicated code in sync with other plugins
// TODO we should probably abstract this into a common function
async #uploadRemoteFile (file) {
// TODO: we could rewrite this to use server-sent events instead of creating WebSockets.
try {
if (file.serverToken) {
return this.connectToServerSocket(file)
return await this.connectToServerSocket(file)
}
const serverToken = await this.#queueRequestSocketToken(file)

if (!this.uppy.getState().files[file.id]) return undefined

this.uppy.setFileState(file.id, { serverToken })
return this.connectToServerSocket(this.uppy.getFile(file.id))
return await this.connectToServerSocket(this.uppy.getFile(file.id))
} catch (err) {
this.uppy.setFileState(file.id, { serverToken: undefined })
this.uppy.emit('upload-error', file, err)
throw err
}
}

connectToServerSocket (file) {
async connectToServerSocket (file) {
return new Promise((resolve, reject) => {
const opts = this.#getOptions(file)
const token = file.serverToken
Expand Down
9 changes: 6 additions & 3 deletions packages/@uppy/tus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ export default class Tus extends BasePlugin {
return res.token
}

// NOTE! Keep this duplicated code in sync with other plugins
// TODO we should probably abstract this into a common function
/**
* @param {UppyFile} file for use with upload
* @returns {Promise<void>}
Expand All @@ -470,15 +472,16 @@ export default class Tus extends BasePlugin {

try {
if (file.serverToken) {
return this.connectToServerSocket(file)
return await this.connectToServerSocket(file)
}
const serverToken = await this.#queueRequestSocketToken(file)

if (!this.uppy.getState().files[file.id]) return undefined

this.uppy.setFileState(file.id, { serverToken })
return this.connectToServerSocket(this.uppy.getFile(file.id))
return await this.connectToServerSocket(this.uppy.getFile(file.id))
} catch (err) {
this.uppy.setFileState(file.id, { serverToken: undefined })
this.uppy.emit('upload-error', file, err)
throw err
}
Expand All @@ -492,7 +495,7 @@ export default class Tus extends BasePlugin {
*
* @param {UppyFile} file
*/
connectToServerSocket (file) {
async connectToServerSocket (file) {
return new Promise((resolve, reject) => {
const token = file.serverToken
const host = getSocketHost(file.remote.companionUrl)
Expand Down
9 changes: 6 additions & 3 deletions packages/@uppy/xhr-upload/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,26 +373,29 @@ export default class XHRUpload extends BasePlugin {
return res.token
}

// NOTE! Keep this duplicated code in sync with other plugins
// TODO we should probably abstract this into a common function
async uploadRemote (file) {
// TODO: we could rewrite this to use server-sent events instead of creating WebSockets.
try {
this.uppy.emit('upload-started', file)
if (file.serverToken) {
return this.connectToServerSocket(file)
return await this.connectToServerSocket(file)
}
const serverToken = await this.#queueRequestSocketToken(file)

if (!this.uppy.getState().files[file.id]) return undefined

this.uppy.setFileState(file.id, { serverToken })
return this.connectToServerSocket(this.uppy.getFile(file.id))
return await this.connectToServerSocket(this.uppy.getFile(file.id))
} catch (err) {
this.uppy.setFileState(file.id, { serverToken: undefined })
this.uppy.emit('upload-error', file, err)
throw err
}
}

connectToServerSocket (file) {
async connectToServerSocket (file) {
return new Promise((resolve, reject) => {
const opts = this.getOptions(file)
const token = file.serverToken
Expand Down

0 comments on commit 643fd8d

Please sign in to comment.