feat: Check images passed as File before making them Image

We don't want images having unsupported format or corrupted ones to be sent as `Image` and appear in
the "Images" tab in UIs because they can't be displayed correctly.
This commit is contained in:
iequidoo
2025-06-09 19:47:20 -03:00
committed by iequidoo
parent 75b7bea78f
commit 3f66ae91cd
4 changed files with 45 additions and 28 deletions

View File

@@ -3727,18 +3727,26 @@ async fn test_nonimage_with_png_ext() -> Result<()> {
let bytes = include_bytes!("../../test-data/message/thunderbird_with_autocrypt.eml");
let file = alice.get_blobdir().join("screenshot.png");
tokio::fs::write(&file, bytes).await?;
let mut msg = Message::new(Viewtype::Image);
msg.set_file_and_deduplicate(alice, &file, Some("screenshot.png"), None)?;
let sent_msg = alice.send_msg(alice_chat.get_id(), &mut msg).await;
assert_eq!(msg.viewtype, Viewtype::File);
assert_eq!(msg.get_filemime().unwrap(), "application/octet-stream");
assert!(!msg.get_filename().unwrap().contains("screenshot"));
let msg_bob = bob.recv_msg(&sent_msg).await;
assert_eq!(msg_bob.viewtype, Viewtype::File);
assert_eq!(msg_bob.get_filemime().unwrap(), "application/octet-stream");
assert!(!msg_bob.get_filename().unwrap().contains("screenshot"));
for vt in [Viewtype::Image, Viewtype::File] {
tokio::fs::write(&file, bytes).await?;
let mut msg = Message::new(vt);
msg.set_file_and_deduplicate(alice, &file, Some("screenshot.png"), None)?;
let sent_msg = alice.send_msg(alice_chat.get_id(), &mut msg).await;
assert_eq!(msg.viewtype, Viewtype::File);
assert_eq!(msg.get_filemime().unwrap(), "application/octet-stream");
assert_eq!(
msg.get_filename().unwrap().contains("screenshot"),
vt == Viewtype::File
);
let msg_bob = bob.recv_msg(&sent_msg).await;
assert_eq!(msg_bob.viewtype, Viewtype::File);
assert_eq!(msg_bob.get_filemime().unwrap(), "application/octet-stream");
assert_eq!(
msg_bob.get_filename().unwrap().contains("screenshot"),
vt == Viewtype::File
);
}
Ok(())
}