Skip to content

refactor: replace hand-rolled MIME lookup with mime_guess2#492

Open
malob wants to merge 1 commit intogoogleworkspace:mainfrom
malob:feat/mime-guess
Open

refactor: replace hand-rolled MIME lookup with mime_guess2#492
malob wants to merge 1 commit intogoogleworkspace:mainfrom
malob:feat/mime-guess

Conversation

@malob
Copy link
Contributor

@malob malob commented Mar 15, 2026

Description

Replace the manual mime_from_extension match table (~25 extensions) in executor.rs with the mime_guess2 crate, which provides comprehensive MIME type detection from the Apache MIME database.

Why

Just as I don't think this package should hand-roll RFC 5322 email construction when battle-tested libraries exist (#491), I don't think it should maintain a partial extension-to-MIME mapping when a well-maintained crate covers the full spectrum. The current match table handles common types but file types like .webp, .mp4, .heic, .wasm, .tar.gz, and hundreds of others fall through to the metadata fallback or application/octet-stream.

mime_guess2 is an actively maintained fork of mime_guess (6.5M downloads, MIT licensed, 24KB). It uses a static lookup table from the Apache MIME database — no filesystem access or magic-byte sniffing.

This also lays the foundation for upcoming Gmail attachment support (--attach flag, #247), which will need MIME detection for email attachments via the same crate.

What changed

  • Added mime_guess2 = "2.3" to Cargo.toml
  • Replaced mime_from_extension() body with mime_guess2::from_path().first()
  • Deleted the hand-rolled match table
  • All 6 existing test_resolve_upload_mime_* tests pass unchanged

Checklist:

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

@malob malob requested a review from jpoehnelt as a code owner March 15, 2026 00:58
@changeset-bot
Copy link

changeset-bot bot commented Mar 15, 2026

🦋 Changeset detected

Latest commit: 55da988

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the MIME type detection logic by adopting the mime_guess2 crate. This enhancement moves away from a limited, manually maintained mapping to a comprehensive, battle-tested library, ensuring more accurate content-type inference for file uploads. The change improves the reliability of file handling and sets the stage for future functionalities requiring broad MIME type support.

Highlights

  • Replaced custom MIME type detection: The hand-rolled mime_from_extension function in src/executor.rs has been replaced with the mime_guess2 crate for more robust and comprehensive MIME type inference.
  • Improved MIME type coverage: This change significantly expands the range of detected MIME types beyond the previous ~25 extensions, supporting types like .webp, .mp4, and .wasm, which previously defaulted to application/octet-stream.
  • Foundation for future features: Integrating mime_guess2 prepares the codebase for upcoming features such as Gmail attachment support, which will rely on accurate MIME detection.
Changelog
  • .changeset/mime-guess-upload.md
    • Added a new changeset file documenting the replacement of the hand-rolled MIME type lookup with mime_guess2.
  • Cargo.toml
    • Added mime_guess2 as a new dependency with version 2.3.
  • src/executor.rs
    • Replaced the call to the custom mime_from_extension function with mime_guess2::from_path().first().
    • Removed the mime_from_extension helper function and its associated match table.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a solid refactoring that replaces a limited, hand-rolled MIME type lookup with the mime_guess2 crate. This change improves maintainability and correctness by leveraging a comprehensive, well-maintained library. The implementation is clean, correct, and properly integrated into the existing logic. I have no high or critical severity issues to report.

Replace the manual mime_from_extension match table with the mime_guess2
crate for comprehensive file extension to MIME type detection.
@malob malob force-pushed the feat/mime-guess branch from 8732299 to 55da988 Compare March 15, 2026 01:00
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the MIME type detection by replacing a hand-rolled lookup table with the mime_guess2 crate. This is a good change that improves maintainability and extends functionality to support a much wider range of file types for uploads. The implementation correctly integrates the new crate while preserving the existing fallback logic. After a thorough review, I found no issues with the proposed changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants