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

Fix impossible blob name check #19

Closed
wants to merge 1 commit into from

Conversation

arcataroger
Copy link
Member

This partially fixes an issue with being able to upload JS Blobs, as reported on the forum: https://community.datocms.com/t/createupload-fails-with-missing-filename/4886/

We had an if statement:

  if (!(options.filename && 'name' in fileOrBlob)) {
    throw new Error('Missing filename, please provide it as an option!');
  }

This would always be false if fileOrBlob was a Blob, because Blobs don't have a name property (only Files do).

@arcataroger arcataroger self-assigned this Mar 22, 2024
Comment on lines +37 to +45
let filename: string;

if (options.filename) {
// If a filename is explicitly provided, use it
filename = options.filename;
} else if (fileOrBlob instanceof File && fileOrBlob.name) {
// Files extend Blobs and may have a `name` property. Blobs never have names.
filename = fileOrBlob.name;
} else {
Copy link
Member Author

@arcataroger arcataroger Mar 22, 2024

Choose a reason for hiding this comment

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

We could've also just changed the && to || in the original if, but then that will cause the later line filename = options.filename || fileOrBlob.name to have a TypeScript error, because blobs can never have names.

I think this is more explicit than adding a ts-ignore

@arcataroger arcataroger added the bug Something isn't working label Mar 22, 2024
@arcataroger arcataroger marked this pull request as ready for review March 22, 2024 00:04
@stefanoverna
Copy link
Member

Fixed here! 580affc

@stefanoverna
Copy link
Member

(good catch btw!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants