fix(services): added plain-text MIME type fallback for consistent file detection#9102
fix(services): added plain-text MIME type fallback for consistent file detection#9102Rupak182 wants to merge 2 commits into
Conversation
📝 WalkthroughWalkthroughThis PR adds a static ChangesFile MIME Type Detection Enhancement
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/services/src/file/helper.ts (1)
84-94: ⚡ Quick winPreserve literal extension keys with
as const satisfies.This map is currently widened via
Record<string, string>, which drops key-level precision. Preferas const satisfiesso keys stay literal while still being type-checked.Suggested change
-const PLAIN_TEXT_MIME_MAP: Record<string, string> = { +const PLAIN_TEXT_MIME_MAP = { md: "text/markdown", markdown: "text/markdown", txt: "text/plain", csv: "text/csv", json: "application/json", css: "text/css", js: "text/javascript", sql: "application/x-sql", xml: "text/xml", -}; +} as const satisfies Record<string, string>;As per coding guidelines, "Use the
satisfiesoperator to validate types without widening them".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/services/src/file/helper.ts` around lines 84 - 94, The PLAIN_TEXT_MIME_MAP object is currently declared with a widened type Record<string, string>, losing literal key types; replace the explicit annotation with a const object literal and append "as const satisfies Record<string, string>" (i.e., remove Record<string, string> from the declaration and use "as const satisfies" on the object) so the extension keys stay literal while still type-checked; update the symbol PLAIN_TEXT_MIME_MAP accordingly wherever used.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/services/src/file/helper.ts`:
- Around line 120-122: PLAIN_TEXT_MIME_MAP lookup uses the `in` operator which
matches inherited properties (so extensions like "toString" could incorrectly
resolve); change the check to use Object.hasOwn(PLAIN_TEXT_MIME_MAP, extension)
(or Object.prototype.hasOwn.call) in the function that reads `extension` and
returns from PLAIN_TEXT_MIME_MAP so only own properties are considered and the
Promise<string> return remains correct.
---
Nitpick comments:
In `@packages/services/src/file/helper.ts`:
- Around line 84-94: The PLAIN_TEXT_MIME_MAP object is currently declared with a
widened type Record<string, string>, losing literal key types; replace the
explicit annotation with a const object literal and append "as const satisfies
Record<string, string>" (i.e., remove Record<string, string> from the
declaration and use "as const satisfies" on the object) so the extension keys
stay literal while still type-checked; update the symbol PLAIN_TEXT_MIME_MAP
accordingly wherever used.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1950d58f-6476-45a4-9c2d-d71eda7bb195
📒 Files selected for processing (1)
packages/services/src/file/helper.ts
Description
Plain Text file like markdown, csv and json cannot be recoginzed with detectMimeTypeFromSignature function based on binary magic numbers and hence earlier an empty string was getting passed to backend and file upload was failing previously . I added plain-text MIME type fallback for consistent file detection in case of plain text files.
Type of Change
Screenshots and Media (if applicable)
Test Scenarios
References
Related Issue: #8565
Summary by CodeRabbit