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

Trim accept tokens as in spec. #1151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/utility/upload.ts
Expand Up @@ -61,13 +61,18 @@ function isAcceptableFile(file: File, accept: string) {

const wildcards = ['audio/*', 'image/*', 'video/*']

return accept.split(',').some(acceptToken => {
if (acceptToken.startsWith('.')) {
// tokens starting with a dot represent a file extension
return file.name.endsWith(acceptToken)
} else if (wildcards.includes(acceptToken)) {
return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1))
}
return file.type === acceptToken
})
return accept
.split(',')
.map(acceptToken => acceptToken.trim())
.some(acceptToken => {
if (acceptToken.startsWith('.')) {
// tokens starting with a dot represent a file extension
return file.name.endsWith(acceptToken)
} else if (wildcards.includes(acceptToken)) {
return file.type.startsWith(
acceptToken.substr(0, acceptToken.length - 1),
)
}
return file.type === acceptToken
})
}
1 change: 1 addition & 0 deletions tests/utility/upload.ts
Expand Up @@ -155,6 +155,7 @@ test.each([
[true, '.png', 1],
[true, 'text/csv', 1],
[true, '', 4],
[true, '.png, .jpg', 3],
[false, 'video/*', 4],
])(
'filter according to accept attribute applyAccept=%s, acceptAttribute=%s',
Expand Down