Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Pass newFile with all the info to onBeforeFileAdded #1594

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions packages/@uppy/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,21 +402,6 @@ class Uppy {
onError(new Error('Cannot add new files: already uploading.'))
}

const onBeforeFileAddedResult = this.opts.onBeforeFileAdded(file, files)

if (onBeforeFileAddedResult === false) {
this.log('Not adding file because onBeforeFileAdded returned false')
return
}

if (typeof onBeforeFileAddedResult === 'object' && onBeforeFileAddedResult) {
// warning after the change in 0.24
if (onBeforeFileAddedResult.then) {
throw new TypeError('onBeforeFileAdded() returned a Promise, but this is no longer supported. It must be synchronous.')
}
file = onBeforeFileAddedResult
}

const fileType = getFileType(file)
let fileName
if (file.name) {
Expand All @@ -437,7 +422,7 @@ class Uppy {

// `null` means the size is unknown.
const size = isFinite(file.data.size) ? file.data.size : null
const newFile = {
let newFile = {
source: file.source || '',
id: fileID,
name: fileName,
Expand Down Expand Up @@ -465,6 +450,17 @@ class Uppy {
onError(err)
}

const onBeforeFileAddedResult = this.opts.onBeforeFileAdded(newFile, files)

if (onBeforeFileAddedResult === false) {
this.log('Not adding file because onBeforeFileAdded returned false')
return
}

if (typeof onBeforeFileAddedResult === 'object' && onBeforeFileAddedResult) {
newFile = onBeforeFileAddedResult
}

this.setState({
files: Object.assign({}, files, {
[fileID]: newFile
Expand Down Expand Up @@ -1244,23 +1240,32 @@ class Uppy {
}

let files = this.getState().files

const handleError = (err) => {
const message = typeof err === 'object' ? err.message : err
const details = typeof err === 'object' ? err.details : null
this.log(`${message} ${details}`)
this.info({ message: message, details: details }, 'error', 4000)
return Promise.reject(typeof err === 'object' ? err : new Error(err))
}

try {
this._checkMinNumberOfFiles(files)
} catch (err) {
return handleError(err)
}

const onBeforeUploadResult = this.opts.onBeforeUpload(files)

if (onBeforeUploadResult === false) {
return Promise.reject(new Error('Not starting the upload because onBeforeUpload returned false'))
}

if (onBeforeUploadResult && typeof onBeforeUploadResult === 'object') {
// warning after the change in 0.24
if (onBeforeUploadResult.then) {
throw new TypeError('onBeforeUpload() returned a Promise, but this is no longer supported. It must be synchronous.')
}

files = onBeforeUploadResult
}

return Promise.resolve()
.then(() => this._checkMinNumberOfFiles(files))
.then(() => {
const { currentUploads } = this.getState()
// get a list of files that are currently assigned to uploads
Expand All @@ -1278,13 +1283,7 @@ class Uppy {
const uploadID = this._createUpload(waitingFileIDs)
return this._runUpload(uploadID)
})
.catch((err) => {
const message = typeof err === 'object' ? err.message : err
const details = typeof err === 'object' ? err.details : null
this.log(`${message} ${details}`)
this.info({ message: message, details: details }, 'error', 4000)
return Promise.reject(typeof err === 'object' ? err : new Error(err))
})
.catch(handleError)
}
}

Expand Down