Return None for unknown MIME types and ignore paste with warning

This commit is contained in:
Frederic Laing 2026-01-16 22:58:47 +01:00
parent 08d442aee2
commit b8ac39ade5
No known key found for this signature in database
GPG key ID: C126157F0CDCD306
2 changed files with 47 additions and 29 deletions

View file

@ -3531,8 +3531,16 @@ impl Application for App {
}); });
} }
Message::PasteImageContents(to, contents) => { Message::PasteImageContents(to, contents) => {
let Some(extension) = contents.extension() else {
log::warn!(
"Ignoring paste: unknown image MIME type {:?}",
contents.mime_type
);
return Task::none();
};
// Generate unique filename for the pasted image // Generate unique filename for the pasted image
let base_name = format!("{}.{}", fl!("pasted-image"), contents.extension()); let base_name = format!("{}.{}", fl!("pasted-image"), extension);
let base_path = to.join(&base_name); let base_path = to.join(&base_name);
let final_path = copy_unique_path(&base_path, &to); let final_path = copy_unique_path(&base_path, &to);
@ -3558,8 +3566,16 @@ impl Application for App {
}); });
} }
Message::PasteVideoContents(to, contents) => { Message::PasteVideoContents(to, contents) => {
let Some(extension) = contents.extension() else {
log::warn!(
"Ignoring paste: unknown video MIME type {:?}",
contents.mime_type
);
return Task::none();
};
// Generate unique filename for the pasted video // Generate unique filename for the pasted video
let base_name = format!("{}.{}", fl!("pasted-video"), contents.extension()); let base_name = format!("{}.{}", fl!("pasted-video"), extension);
let base_path = to.join(&base_name); let base_path = to.join(&base_name);
let final_path = copy_unique_path(&base_path, &to); let final_path = copy_unique_path(&base_path, &to);

View file

@ -211,21 +211,22 @@ impl TryFrom<(Vec<u8>, String)> for ClipboardPasteImage {
impl ClipboardPasteImage { impl ClipboardPasteImage {
/// Get the file extension for the image based on MIME type. /// Get the file extension for the image based on MIME type.
pub fn extension(&self) -> &'static str { /// Returns None if the MIME type is not recognized.
pub fn extension(&self) -> Option<&'static str> {
match self.mime_type.as_str() { match self.mime_type.as_str() {
"image/png" | "image/x-png" => "png", "image/png" | "image/x-png" => Some("png"),
"image/jpeg" | "image/pjpeg" => "jpg", "image/jpeg" | "image/pjpeg" => Some("jpg"),
"image/gif" => "gif", "image/gif" => Some("gif"),
"image/bmp" | "image/x-bmp" | "image/x-ms-bmp" => "bmp", "image/bmp" | "image/x-bmp" | "image/x-ms-bmp" => Some("bmp"),
"image/webp" => "webp", "image/webp" => Some("webp"),
"image/tiff" | "image/x-tiff" => "tiff", "image/tiff" | "image/x-tiff" => Some("tiff"),
"image/svg+xml" => "svg", "image/svg+xml" => Some("svg"),
"image/x-icon" | "image/vnd.microsoft.icon" => "ico", "image/x-icon" | "image/vnd.microsoft.icon" => Some("ico"),
"image/avif" => "avif", "image/avif" => Some("avif"),
"image/heic" => "heic", "image/heic" => Some("heic"),
"image/heif" => "heif", "image/heif" => Some("heif"),
"image/jxl" => "jxl", "image/jxl" => Some("jxl"),
_ => "png", // Default to png _ => None,
} }
} }
} }
@ -272,20 +273,21 @@ impl TryFrom<(Vec<u8>, String)> for ClipboardPasteVideo {
impl ClipboardPasteVideo { impl ClipboardPasteVideo {
/// Get the file extension for the video based on MIME type. /// Get the file extension for the video based on MIME type.
pub fn extension(&self) -> &'static str { /// Returns None if the MIME type is not recognized.
pub fn extension(&self) -> Option<&'static str> {
match self.mime_type.as_str() { match self.mime_type.as_str() {
"video/mp4" => "mp4", "video/mp4" => Some("mp4"),
"video/webm" => "webm", "video/webm" => Some("webm"),
"video/ogg" => "ogv", "video/ogg" => Some("ogv"),
"video/mpeg" => "mpeg", "video/mpeg" => Some("mpeg"),
"video/quicktime" => "mov", "video/quicktime" => Some("mov"),
"video/x-msvideo" | "video/avi" => "avi", "video/x-msvideo" | "video/avi" => Some("avi"),
"video/x-matroska" => "mkv", "video/x-matroska" => Some("mkv"),
"video/x-flv" => "flv", "video/x-flv" => Some("flv"),
"video/3gpp" => "3gp", "video/3gpp" => Some("3gp"),
"video/3gpp2" => "3g2", "video/3gpp2" => Some("3g2"),
"video/x-ms-wmv" => "wmv", "video/x-ms-wmv" => Some("wmv"),
_ => "mp4", // Default to mp4 _ => None,
} }
} }
} }