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

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

Merged
merged 4 commits into from
Aug 7, 2023

Conversation

Murderlon
Copy link
Member

Closes #4470

  • Change default signature of onBeforeFileAdded to check for duplicate files to not make this a breaking change.
  • Swap order of checkIfFileAlreadyExists and onBeforeFileAdded.

This means:

  • If you add the same file again before uploading, it will override it.
  • If you add the same file after uploading, you can upload it again. Depending on your uploader and backend this may or may not override the file. Tus for instance always generates unique IDs so it wouldn't override it. But people should be able to handle it the way they want.

Needs docs PR too.

@Murderlon Murderlon requested review from mifi, arturi and aduh95 July 20, 2023 10:28
@Murderlon Murderlon self-assigned this Jul 20, 2023
Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for the default behavior of adding the same file twice? If not, can we add that test case?

@Murderlon
Copy link
Member Author

Do we have a test for the default behavior of adding the same file twice? If not, can we add that test case?

it('should not allow a dupicate file, a file with the same id', () => {
const core = new Core()
const sameFileBlob = new File([sampleImage], { type: 'image/jpeg' })
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: sameFileBlob,
})
expect(() => {
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: sameFileBlob,
meta: {
notARelativePath: 'folder/a',
},
})
}).toThrow(
"Cannot add the duplicate file 'foo.jpg', it already exists",
)
expect(core.getFiles().length).toEqual(1)
})
it('should allow a duplicate file if its relativePath is different, thus the id is different', () => {
const core = new Core()
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new File([sampleImage], { type: 'image/jpeg' }),
})
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new File([sampleImage], { type: 'image/jpeg' }),
meta: {
relativePath: 'folder/a',
},
})
expect(core.getFiles().length).toEqual(2)
})
it('should not allow a file if onBeforeFileAdded returned false', () => {
const core = new Core({
onBeforeFileAdded: (file) => {
if (file.source === 'jest') {
return false
}
return undefined
},
})
expect(() => {
core.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new File([sampleImage], { type: 'image/jpeg' }),
})
}).toThrow(
'Cannot add the file because onBeforeFileAdded returned false.',
)
expect(core.getFiles().length).toEqual(0)
})

@aduh95
Copy link
Contributor

aduh95 commented Jul 20, 2023

Would it make sense to put those two tests next to each other?

@Murderlon
Copy link
Member Author

It's already in the same describe block about adding new files, just below the other test for onBeforeFileAdded.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to have the two tests next to one another, it would also be the occasion to fix the typo dupicate -> duplicate, but no strong feelings

packages/@uppy/core/src/Uppy.js Outdated Show resolved Hide resolved
packages/@uppy/core/src/Uppy.js Show resolved Hide resolved
@Murderlon Murderlon requested a review from mifi August 3, 2023 13:44
@mifi
Copy link
Contributor

mifi commented Aug 6, 2023

othar than that, lgtm!

@Murderlon Murderlon merged commit aa3f03f into main Aug 7, 2023
@Murderlon Murderlon deleted the allow-duplicate-files branch August 7, 2023 09:23
github-actions bot added a commit that referenced this pull request Aug 15, 2023
| Package                   | Version | Package                   | Version |
| ------------------------- | ------- | ------------------------- | ------- |
| @uppy/audio               |   1.1.2 | @uppy/locales             |   3.3.0 |
| @uppy/aws-s3              |   3.2.2 | @uppy/onedrive            |   3.1.3 |
| @uppy/aws-s3-multipart    |   3.5.3 | @uppy/progress-bar        |   3.0.3 |
| @uppy/box                 |   2.1.3 | @uppy/provider-views      |   3.5.0 |
| @uppy/companion           |   4.8.0 | @uppy/redux-dev-tools     |   3.0.3 |
| @uppy/companion-client    |   3.3.0 | @uppy/screen-capture      |   3.1.2 |
| @uppy/core                |   3.4.0 | @uppy/status-bar          |   3.2.4 |
| @uppy/dashboard           |   3.5.1 | @uppy/thumbnail-generator |   3.0.4 |
| @uppy/drag-drop           |   3.0.3 | @uppy/transloadit         |   3.2.1 |
| @uppy/dropbox             |   3.1.3 | @uppy/tus                 |   3.1.3 |
| @uppy/facebook            |   3.1.2 | @uppy/unsplash            |   3.2.2 |
| @uppy/file-input          |   3.0.3 | @uppy/url                 |   3.3.3 |
| @uppy/google-drive        |   3.2.1 | @uppy/webcam              |   3.3.2 |
| @uppy/image-editor        |   2.1.3 | @uppy/xhr-upload          |   3.3.2 |
| @uppy/informer            |   3.0.3 | @uppy/zoom                |   2.1.2 |
| @uppy/instagram           |   3.1.2 | uppy                      |  3.14.0 |

- meta: Readme improvements (Artur Paikin / #4622)
- @uppy/companion: Fix typos and add env vars to .env.example (Dominik Schmidt / #4624)
- @uppy/aws-s3-multipart: pass the `uploadURL` back to the caller (Antoine du Hamel / #4614)
- meta: update to node-18.17.0-alpine,  (odselsevier / #4617)
- @uppy/aws-s3,@uppy/aws-s3-multipart: update types (Antoine du Hamel / #4611)
- @uppy/aws-s3-multipart,@uppy/aws-s3,@uppy/companion,@uppy/transloadit,@uppy/xhr-upload: use uppercase HTTP method names (Antoine du Hamel / #4612)
- meta: e2e: fix race condition in transloadit test (Antoine du Hamel / #4616)
- @uppy/aws-s3,@uppy/aws-s3-multipart: update types (bdirito / #4576)
- @uppy/core: allow duplicate files with onBeforeFileAdded (Merlijn Vos / #4594)
- @uppy/companion: make CSRF protection helpers available to providers (Dominik Schmidt / #4554)
- @uppy/companion: fix Redis key default TTL (Subha Sarkar / #4607)
- @uppy/companion: Fix Uploader.js metadata normalisation (Subha Sarkar / #4608)
- @uppy/companion-client,@uppy/provider-views: make authentication optional (Dominik Schmidt / #4556)
- @uppy/provider-views: fix ProviderView error on empty plugin.icon (Dominik Schmidt / #4553)
- @uppy/aws-s3,@uppy/tus,@uppy/xhr-upload:  Invoke headers function for remote uploads (Dominik Schmidt / #4596)
- @uppy/companion: Unify redis initialization (Dominik Schmidt / #4597)
- meta: lock node-js version on ci (Mikael Finstad / #4606)
- @uppy/companion: allow dynamic S3 bucket (rmoura-92 / #4579)
- @uppy/status-bar: e2e: add test for retrying and pausing uploads (Antoine du Hamel / #3599)
- meta: e2e: remove too short timeout (Antoine du Hamel / #4602)
Murderlon added a commit that referenced this pull request Aug 24, 2023
* main: (28 commits)
  Release: [email protected] (#4644)
  @uppy/aws-s3-multipart: fix types when using deprecated option (#4634)
  @uppy/companion: harden lint rules (#4641)
  allow empty objects for `fields` types (#4631)
  meta: upgrade Node.js docker version (#4630)
  Release: [email protected] (#4626)
  Readme improvements (#4622)
  Fix typos and add env vars to .env.example (#4624)
  @uppy/aws-s3-multipart: pass the `uploadURL` back to the caller (#4614)
  update to node-18.17.0-alpine,  (#4617)
  @uppy/aws-s3,@uppy/aws-s3-multipart: update types (#4611)
  use uppercase HTTP method names (#4612)
  e2e: fix race condition in transloadit test (#4616)
  @uppy/aws-s3,@uppy/aws-s3-multipart: update types (#4576)
  @uppy/core: allow duplicate files with onBeforeFileAdded (#4594)
  @uppy/companion: make CSRF protection helpers available to providers (#4554)
  @uppy/companion: fix Redis key default TTL (#4607)
  @uppy/companion: Fix Uploader.js metadata normalisation (#4608)
  @uppy/companion-client,@uppy/provider-views: make authentication optional (#4556)
  @uppy/provider-views: fix ProviderView error on empty plugin.icon (#4553)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use onBeforeFileAdded to allow duplicate files
3 participants