-
Notifications
You must be signed in to change notification settings - Fork 68
feat: support file extension in uploader module #425
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
Conversation
WalkthroughThe changes extend file upload validation in FluentEditor by enabling the mimetypes configuration to accept both MIME types and file extensions. The validation logic is updated to support exact MIME matching, wildcard types, and extension-based file matching. Documentation is clarified to reflect the expanded capabilities. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/fluent-editor/src/modules/custom-uploader.ts`:
- Around line 44-55: The mimetype check in custom-uploader.ts mishandles the
default '*' (it falls into the extension branch and rejects files) and builds
unescaped, unanchored regexes for MIME types which can over-match; update the
mimeOk logic used by this.options.mimetypes to: treat a type value of '*' as an
immediate match, detect MIME patterns by presence of '/' but escape regex
metacharacters before replacing '*' -> '.*' and anchor the regex (e.g.,
'^...$'), use a normalized source like (file.type || '').toLowerCase() for MIME
matching, keep the existing extension branch (normalize leading dot and compare
lowercase suffix), and still combine the final mimeOk with the existing size
check.
| const mimeOk = this.options.mimetypes.some((type) => { | ||
| // 简单区分:带 '/' 的按 MIME,其他按后缀 | ||
| if (type.includes('/')) { | ||
| return (file.type || 'text/plain').match(type.replaceAll('*', '.*')) | ||
| } | ||
| else { | ||
| // 按文件名后缀匹配,例如 'png' / '.png' | ||
| const ext = type.startsWith('.') ? type.toLowerCase() : `.${type.toLowerCase()}` | ||
| return file.name.toLowerCase().endsWith(ext) | ||
| } | ||
| }) | ||
| return mimeOk && file.size < this.options.maxSize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix wildcard default and tighten MIME matching.
Line 44-55: default mimetypes: ['*'] now routes to the extension branch and rejects almost all files. Also, building a regex from raw MIME strings without escaping/anchoring can over‑match (e.g., dots match any char). This changes behavior and loosens validation.
🐛 Proposed fix
- const mimeOk = this.options.mimetypes.some((type) => {
+ const mimeOk = this.options.mimetypes.some((type) => {
+ const normalized = type.trim().toLowerCase()
+ if (normalized === '*' || normalized === '*/*') {
+ return true
+ }
// 简单区分:带 '/' 的按 MIME,其他按后缀
- if (type.includes('/')) {
- return (file.type || 'text/plain').match(type.replaceAll('*', '.*'))
+ if (normalized.includes('/')) {
+ const escaped = normalized
+ .replace(/[.+?^${}()|[\]\\]/g, '\\$&')
+ .replaceAll('\\*', '.*')
+ const re = new RegExp(`^${escaped}$`, 'i')
+ return re.test(file.type || '')
}
else {
// 按文件名后缀匹配,例如 'png' / '.png'
- const ext = type.startsWith('.') ? type.toLowerCase() : `.${type.toLowerCase()}`
+ const ext = normalized.startsWith('.') ? normalized : `.${normalized}`
return file.name.toLowerCase().endsWith(ext)
}
})🤖 Prompt for AI Agents
In `@packages/fluent-editor/src/modules/custom-uploader.ts` around lines 44 - 55,
The mimetype check in custom-uploader.ts mishandles the default '*' (it falls
into the extension branch and rejects files) and builds unescaped, unanchored
regexes for MIME types which can over-match; update the mimeOk logic used by
this.options.mimetypes to: treat a type value of '*' as an immediate match,
detect MIME patterns by presence of '/' but escape regex metacharacters before
replacing '*' -> '.*' and anchor the regex (e.g., '^...$'), use a normalized
source like (file.type || '').toLowerCase() for MIME matching, keep the existing
extension branch (normalize leading dot and compare lowercase suffix), and still
combine the final mimeOk with the existing size check.
PR
close #424
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.