Skip to content

Commit 55da988

Browse files
committed
refactor: replace hand-rolled MIME lookup with mime_guess2
Replace the manual mime_from_extension match table with the mime_guess2 crate for comprehensive file extension to MIME type detection.
1 parent 1308786 commit 55da988

4 files changed

Lines changed: 38 additions & 32 deletions

File tree

.changeset/mime-guess-upload.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@googleworkspace/cli": patch
3+
---
4+
5+
Replace hand-rolled MIME type lookup with mime_guess2 crate for upload content-type inference

Cargo.lock

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ crossterm = "0.29.0"
5656
chrono = "0.4.44"
5757
chrono-tz = "0.10"
5858
iana-time-zone = "0.1"
59+
mime_guess2 = "2.3"
5960
async-trait = "0.1.89"
6061
serde_yaml = "0.9.34"
6162
percent-encoding = "2.3.2"

src/executor.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ fn resolve_upload_mime(
808808
}
809809

810810
if let Some(path) = upload_path {
811-
if let Some(detected) = mime_from_extension(path) {
811+
if let Some(detected) = mime_guess2::from_path(path).first() {
812812
return detected.to_string();
813813
}
814814
}
@@ -824,33 +824,6 @@ fn resolve_upload_mime(
824824
"application/octet-stream".to_string()
825825
}
826826

827-
/// Infers a MIME type from a file path's extension.
828-
fn mime_from_extension(path: &str) -> Option<&'static str> {
829-
let ext = std::path::Path::new(path)
830-
.extension()
831-
.and_then(|e| e.to_str())?;
832-
match ext.to_lowercase().as_str() {
833-
"md" | "markdown" => Some("text/markdown"),
834-
"html" | "htm" => Some("text/html"),
835-
"txt" => Some("text/plain"),
836-
"json" => Some("application/json"),
837-
"csv" => Some("text/csv"),
838-
"xml" => Some("application/xml"),
839-
"pdf" => Some("application/pdf"),
840-
"png" => Some("image/png"),
841-
"jpg" | "jpeg" => Some("image/jpeg"),
842-
"gif" => Some("image/gif"),
843-
"svg" => Some("image/svg+xml"),
844-
"doc" => Some("application/msword"),
845-
"docx" => Some("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
846-
"xls" => Some("application/vnd.ms-excel"),
847-
"xlsx" => Some("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
848-
"ppt" => Some("application/vnd.ms-powerpoint"),
849-
"pptx" => Some("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
850-
_ => None,
851-
}
852-
}
853-
854827
/// Builds a streaming multipart/related body for media upload requests.
855828
///
856829
/// Instead of reading the entire file into memory, this streams the file in

0 commit comments

Comments
 (0)