Skip to content

Commit

Permalink
Don't rely on mimetypes to load image data (#58)
Browse files Browse the repository at this point in the history
Mimetypes are not always correct. Since it's cheap to check the
first few bytes of the image data array to determine the image file format,
we just always do that up front, and ignore the mimetype entirely.
  • Loading branch information
kevinaboos authored Mar 15, 2024
1 parent 94fdf85 commit 592199d
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/home/room_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,8 @@ fn populate_message_view(
(item, true)
} else {
// We don't use thumbnails, as their resolution is too low to be visually useful.
let (mimetype, _width, _height) = if let Some(info) = image.info.as_ref() {
// We also don't trust the provided mimetype, as it can be incorrect.
let (_mimetype, _width, _height) = if let Some(info) = image.info.as_ref() {
(
info.mimetype.as_deref().and_then(utils::ImageFormat::from_mimetype),
info.width,
Expand All @@ -1125,15 +1126,11 @@ fn populate_message_view(
let text_or_image_ref = item.text_or_image(id!(content.message));
match &image.source {
MediaSource::Plain(mxc_uri) => {
// now that we've obtained the image URI and its mimetype, try to fetch the image.
// now that we've obtained the image URI and its metadata, try to fetch the image.
match media_cache.try_get_media_or_fetch(mxc_uri.clone(), None) {
MediaCacheEntry::Loaded(data) => {
let set_image_result = text_or_image_ref.set_image(|img|
match mimetype {
Some(utils::ImageFormat::Png) => img.load_png_from_data(cx, &data),
Some(utils::ImageFormat::Jpeg) => img.load_jpg_from_data(cx, &data),
_unknown => utils::load_png_or_jpg(&img, cx, &data),
}
let set_image_result = text_or_image_ref.set_image(
|img| utils::load_png_or_jpg(&img, cx, &data)
);
if let Err(e) = set_image_result {
let err_str = format!("Failed to display image: {e:?}");
Expand Down

0 comments on commit 592199d

Please sign in to comment.