Skip to content

fix(services): added plain-text MIME type fallback for consistent file detection#9102

Open
Rupak182 wants to merge 2 commits into
makeplane:previewfrom
Rupak182:fix/plain-text-mime-fallback
Open

fix(services): added plain-text MIME type fallback for consistent file detection#9102
Rupak182 wants to merge 2 commits into
makeplane:previewfrom
Rupak182:fix/plain-text-mime-fallback

Conversation

@Rupak182
Copy link
Copy Markdown

@Rupak182 Rupak182 commented May 18, 2026

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

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Improvement (change that would cause existing functionality to not work as expected)
  • Code refactoring
  • Performance improvements
  • Documentation update

Screenshots and Media (if applicable)

image

Test Scenarios

References

Related Issue: #8565

Summary by CodeRabbit

  • Bug Fixes
    • Improved file type detection for plain-text uploads (e.g., markdown, txt, csv, json, css, js, sql, xml). When signature-based detection fails, the system now checks a trusted extension-to-MIME mapping and otherwise uses the OS/browser-provided type, resulting in more reliable MIME identification during file uploads.

Review Change Stack

Copilot AI review requested due to automatic review settings May 18, 2026 17:49
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

📝 Walkthrough

Walkthrough

This PR adds a static PLAIN_TEXT_MIME_MAP and updates validateAndDetectFileType to use it as a fallback when signature-based MIME detection yields no result, otherwise returning file.type || "".

Changes

File MIME Type Detection Enhancement

Layer / File(s) Summary
Plain-text MIME fallback mapping
packages/services/src/file/helper.ts
PLAIN_TEXT_MIME_MAP provides static MIME detection for whitelisted plain-text extensions (markdown, txt, csv, json, css, js, sql, xml). validateAndDetectFileType now returns the mapped MIME when present after signature detection fails, otherwise uses `file.type

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A tiny map hops to the fore,
When signatures miss, it finds the core.
Extensions whisper what files might be,
Now plain text shows its true degree.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a plain-text MIME type fallback for file detection, which matches the core objective of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description includes all major required sections: Description, Type of Change, Screenshots, and References. However, the Test Scenarios section is empty and incomplete.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 18, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a 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

🧹 Nitpick comments (1)
packages/services/src/file/helper.ts (1)

84-94: ⚡ Quick win

Preserve literal extension keys with as const satisfies.

This map is currently widened via Record<string, string>, which drops key-level precision. Prefer as const satisfies so 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 satisfies operator 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

📥 Commits

Reviewing files that changed from the base of the PR and between 50a7b47 and 5325ac1.

📒 Files selected for processing (1)
  • packages/services/src/file/helper.ts

Comment thread packages/services/src/file/helper.ts Outdated
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.

2 participants