diff --git a/Cargo.toml b/Cargo.toml index dfd81c967..4732335cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -589,6 +589,9 @@ version = "0.10.4" default-features = false features = ["std"] +[workspace.dependencies.mime] +version = "0.3.18" + [workspace.dependencies.nonzero_ext] version = "0.3.0" diff --git a/src/service/Cargo.toml b/src/service/Cargo.toml index 41d1220fd..28c1f78a9 100644 --- a/src/service/Cargo.toml +++ b/src/service/Cargo.toml @@ -105,6 +105,7 @@ itertools.workspace = true log.workspace = true loole.workspace = true lru-cache.workspace = true +mime.workspace = true nonzero_ext.workspace = true p256.workspace = true regex.workspace = true diff --git a/src/service/media/preview.rs b/src/service/media/preview.rs index 9a5d60a50..f71f79ba2 100644 --- a/src/service/media/preview.rs +++ b/src/service/media/preview.rs @@ -7,16 +7,25 @@ use std::time::SystemTime; -use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt, info}; +use conduwuit::{Err, Result, debug, err, info, utils::response::LimitReadExt}; use ipaddress::IPAddress; -use lettre::message::header::ContentType; +use mime::Mime; use ruma::OwnedMxcUri; -use serde::Serialize; +use serde::{Serialize, Serializer}; use url::Url; use super::Service; use crate::media::mxc::Mxc; +// serde `serialize_with` passes `&Option` for `Option` fields. +#[allow(clippy::ref_option)] +fn serialize_mime(mime: &Option, serializer: S) -> Result { + match mime { + | Some(m) => serializer.serialize_str(m.as_ref()), + | None => serializer.serialize_none(), + } +} + #[derive(Serialize, Default, Debug)] pub struct UrlPreviewData { // From https://ogp.me/ @@ -50,8 +59,12 @@ pub struct UrlPreviewData { #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image"))] pub image: Option, /// A MIME type for this image. - #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:type"))] - pub image_type: Option, + #[serde( + skip_serializing_if = "Option::is_none", + serialize_with = "serialize_mime", + rename(serialize = "og:image:type") + )] + pub image_type: Option, #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:image:size"))] pub image_size: Option, /// The number of pixels wide. @@ -73,8 +86,12 @@ pub struct UrlPreviewData { pub video_width: Option, #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:height"))] pub video_height: Option, - #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:type"))] - pub video_type: Option, + #[serde( + skip_serializing_if = "Option::is_none", + serialize_with = "serialize_mime", + rename(serialize = "og:video:type") + )] + pub video_type: Option, #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:alt"))] pub video_alt: Option, /// A URL to an audio file to accompany this object. @@ -145,16 +162,18 @@ impl Service { ))); }; - let content_type = content_type + let content_type: Mime = content_type .to_str() + .map_err(|e| err!(Request(Unknown("Unknown or invalid Content-Type header: {e}"))))? + .parse() .map_err(|e| err!(Request(Unknown("Unknown or invalid Content-Type header: {e}"))))?; - let data = match content_type { - | html if html.starts_with("text/html") => self.download_html(url.as_str()).await?, - | img if img.starts_with("image/") => self.download_image(url.as_str()).await?, - | video if video.starts_with("video/") => self.download_video(url.as_str()).await?, - | audio if audio.starts_with("audio/") => self.download_audio(url.as_str()).await?, - | other => return Err!(Request(Unknown("Unsupported Content-Type: {other}"))), + let data = match (content_type.type_(), content_type.subtype()) { + | (mime::TEXT, mime::HTML) => self.download_html(url.as_str()).await?, + | (mime::IMAGE, _) => self.download_image(url.as_str()).await?, + | (mime::VIDEO, _) => self.download_video(url.as_str()).await?, + | (mime::AUDIO, _) => self.download_audio(url.as_str()).await?, + | _ => return Err!(Request(Unknown("Unsupported Content-Type: {content_type}"))), }; self.set_url_preview(url.as_str(), &data).await?;