@uppy/core: allow duplicate files with onBeforeFileAdded (#4594)

This commit is contained in:
Merlijn Vos 2023-08-07 17:23:11 +08:00 committed by GitHub
parent 22583dc24b
commit aa3f03f033
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View file

@ -59,7 +59,7 @@ class Uppy {
debug: false,
restrictions: defaultRestrictionOptions,
meta: {},
onBeforeFileAdded: (currentFile) => currentFile,
onBeforeFileAdded: (file, files) => !Object.hasOwn(files, file.id),
onBeforeUpload: (files) => files,
store: new DefaultStore(),
logger: justErrorsLogger,
@ -547,12 +547,12 @@ class Uppy {
this.log(`Replaced the blob in the restored ghost file: ${newFile.name}, ${newFile.id}`)
}
if (this.checkIfFileAlreadyExists(newFile.id)) {
const onBeforeFileAddedResult = this.opts.onBeforeFileAdded(newFile, nextFilesState)
if (!onBeforeFileAddedResult && this.checkIfFileAlreadyExists(newFile.id)) {
throw new RestrictionError(this.i18n('noDuplicates', { fileName: newFile.name }), { file: fileToAdd })
}
const onBeforeFileAddedResult = this.opts.onBeforeFileAdded(newFile, nextFilesState)
if (onBeforeFileAddedResult === false) {
// Dont show UI info for this error, as it should be done by the developer
throw new RestrictionError('Cannot add the file because onBeforeFileAdded returned false.', { isUserFacing: false, file: fileToAdd })

View file

@ -702,6 +702,25 @@ describe('src/Core', () => {
expect(onBeforeFileAdded.mock.calls[0][1]).toEqual({})
})
it('should allow uploading duplicate file if explicitly allowed in onBeforeFileAdded', async () => {
const core = new Core({ onBeforeFileAdded: () => true })
const sameFileBlob = new File([sampleImage], { type: 'image/jpeg' })
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: sameFileBlob,
})
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: sameFileBlob,
})
})
it('should add a file', () => {
const fileData = new File([sampleImage], { type: 'image/jpeg' })
const fileAddedEventMock = jest.fn()