Skip to content

Commit

Permalink
Add guess_mime to BoxedEmbedMediaExt
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Dec 17, 2024
1 parent 54fe6cf commit 547922f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ rusqlite = { version = "0.32.0", optional = true }
crc32fast = { version = "1", optional = true }
ciborium = { version = "0.2", optional = true }

mime_guess = { version = "2", optional = true }

url = { version = "2", optional = true }
thiserror = { version = "2", optional = true, default-features = false }
mime = { version = "0.3", optional = true }
Expand Down
19 changes: 19 additions & 0 deletions src/models/embed/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ pub trait BoxedEmbedMediaExt {
fn with_dims(self, width: i32, height: i32) -> Self;
fn with_mime(self, mime: impl Into<SmolStr>) -> Self;
fn with_description(self, description: impl Into<ThinString>) -> Self;

/// Guess the mime type based on the URL extension
#[cfg(feature = "mime_guess")]
fn guess_mime(self) -> Self;
}

impl BoxedEmbedMediaExt for Box<EmbedMedia> {
Expand Down Expand Up @@ -355,6 +359,21 @@ impl BoxedEmbedMediaExt for Box<EmbedMedia> {
self.description = Some(description.into());
self
}

#[cfg(feature = "mime_guess")]
fn guess_mime(mut self) -> Self {
use smol_str::ToSmolStr;

if self.mime.is_none() {
if let Some((_, ext)) = self.url.rsplit_once('.') {
if let Some(mime) = mime_guess::from_ext(ext).first() {
self.mime = Some(ToSmolStr::to_smolstr(&mime));
}
}
}

self
}
}

pub trait VisitMedia {
Expand Down

0 comments on commit 547922f

Please sign in to comment.