Skip to content

feat: add --upload-content-type flag and smart MIME inference for uploads#429

Open
Huglo wants to merge 1 commit intogoogleworkspace:mainfrom
Huglo:feat/upload-content-type
Open

feat: add --upload-content-type flag and smart MIME inference for uploads#429
Huglo wants to merge 1 commit intogoogleworkspace:mainfrom
Huglo:feat/upload-content-type

Conversation

@Huglo
Copy link

@Huglo Huglo commented Mar 12, 2026

Summary

  • Adds --upload-content-type flag for multipart uploads, allowing the media Content-Type to be set independently from the metadata mimeType
  • Infers media MIME type from file extension (e.g. .mdtext/markdown) when the flag is omitted, so Drive import conversions work automatically
  • Falls back to metadata mimeType only for unrecognized extensions, preserving backward compatibility

Previously, uploading notes.md with "mimeType":"application/vnd.google-apps.document" would label the media bytes as a Google Doc. Now the media part correctly gets text/markdown, and Drive performs the Markdown → Google Docs conversion.

Test plan

Made with Cursor

…oads

The multipart upload media Content-Type is now resolved independently
from the metadata mimeType, enabling Drive import conversions (e.g.
Markdown → Google Docs) to work automatically.

Priority order for the media MIME type:
1. --upload-content-type flag (explicit override)
2. File extension inference (best guess for what the bytes are)
3. Metadata mimeType (backward-compat fallback)
4. application/octet-stream

Previously the metadata mimeType was reused for the media part, which
meant uploading `notes.md` with mimeType set to
`application/vnd.google-apps.document` would incorrectly label the
bytes as a Google Doc instead of text/markdown.

Made-with: Cursor
@Huglo Huglo requested a review from jpoehnelt as a code owner March 12, 2026 13:19
@changeset-bot
Copy link

changeset-bot bot commented Mar 12, 2026

🦋 Changeset detected

Latest commit: a4bfb58

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 Minor

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

@googleworkspace-bot googleworkspace-bot added area: http area: core Core CLI parsing, commands, error handling, utilities labels Mar 12, 2026
@google-cla
Copy link

google-cla bot commented Mar 12, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 significantly enhances the file upload functionality by introducing a dedicated flag for specifying the media's content type and implementing intelligent MIME type inference. These changes streamline the process of uploading files to services like Google Drive, particularly for scenarios requiring automatic format conversions, by ensuring the correct content type is communicated for the actual file bytes, independent of the target metadata type.

Highlights

  • Explicit Content-Type Flag: Introduced a new --upload-content-type flag to explicitly set the media Content-Type for multipart uploads, allowing it to be independent of the metadata mimeType.
  • Smart MIME Inference: Implemented automatic inference of the media MIME type from the file extension (e.g., .md to text/markdown) when the --upload-content-type flag is not provided.
  • Improved Drive Conversions: Enabled automatic import conversions in Google Drive by correctly setting the media Content-Type based on file content, allowing files like Markdown to be converted to Google Docs during upload.
  • Backward Compatibility: Ensured backward compatibility by falling back to the metadata mimeType only when the file extension is unrecognized for MIME inference.
Changelog
  • .changeset/upload-content-type.md
    • Added a new changeset file documenting the --upload-content-type flag and smart MIME inference feature.
  • src/commands.rs
    • Added the --upload-content-type command-line argument to methods supporting media uploads.
  • src/executor.rs
    • Modified build_http_request to accept the new upload_content_type parameter.
    • Implemented resolve_upload_mime function to determine the media MIME type based on explicit flag, file extension, or metadata.
    • Added mime_from_extension helper function to infer MIME types from file extensions.
    • Updated build_multipart_body to use the resolved media MIME type.
    • Adjusted test calls to build_multipart_body and execute_method to accommodate the new parameter.
    • Added new unit tests for resolve_upload_mime covering various scenarios (explicit, extension, metadata fallback, unknown extension, no metadata).
  • src/helpers/calendar.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/chat.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/docs.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/drive.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/gmail/mod.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/script.rs
    • Updated execute_method call to include the new upload_content_type parameter (passed as None).
  • src/helpers/sheets.rs
    • Updated execute_method calls to include the new upload_content_type parameter (passed as None).
  • src/main.rs
    • Parsed the new --upload-content-type command-line argument.
    • Passed the upload_content_type to the execute_method function.
    • Updated the usage printout to include the new flag.
Activity
  • Implemented the --upload-content-type flag and smart MIME inference.
  • Ran cargo test, with 556 tests passing, including 7 new tests for resolve_upload_mime.
  • Executed cargo clippy -- -D warnings, resulting in a clean output.
  • Verified no overlap with existing PRs, specifically noting fix: stream multipart uploads to avoid OOM on large files #418.
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 introduces a valuable feature for file uploads by adding the --upload-content-type flag and intelligent MIME type inference. The implementation is clean and well-tested, and the separation of concerns in resolve_upload_mime is good. I have one suggestion to improve the robustness of the file extension detection logic to better handle common edge cases like dotfiles.

Comment on lines +778 to +780
let ext = std::path::Path::new(path)
.extension()
.and_then(|e| e.to_str())?;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current implementation using std::path::Path::extension doesn't handle dotfiles (e.g., .bashrc, .gitignore) as having an extension, which can lead to incorrect MIME type inference for such files. Using rsplit_once on the filename is more robust and correctly identifies extensions for dotfiles. This ensures that MIME type inference works as expected for a wider range of common file naming conventions.

    let ext = std::path::Path::new(path)
        .file_name()
        .and_then(std::ffi::OsStr::to_str)
        .and_then(|name| name.rsplit_once('.'))
        .map(|(_, ext)| ext)?;

Copy link
Author

Choose a reason for hiding this comment

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

The divergence only happens for dotfiles with no stem (.md, .bashrc, .gitignore). The two questions are:

  1. Would rsplit_once actually help? For .bashrc and .gitignore, it would yield bashrc and gitignore — neither of which is in our MIME map, so the result is identical (None → fallback). The only real case is a bare .md or .json file with no name, which is extremely uncommon as an upload target.
  2. Would it hurt? It could arguably be less correct — .bashrc is conventionally not considered to have an extension; Rust's Path behavior here aligns with POSIX conventions where leading-dot files are hidden files, not extensionless files with a dotted extension.

I would suggest to leave the code as is, but happy to take another point of view.

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

Labels

area: core Core CLI parsing, commands, error handling, utilities area: http

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants