Browse Source

media-viewer: Use the type of the event as the content type of the media

pipelines/767384
Kévin Commaille 1 year ago
parent
commit
efeb245066
No known key found for this signature in database
GPG Key ID: C971D9DBC9D678D
  1. 42
      src/components/media/content_viewer.rs
  2. 2
      src/session/view/content/room_history/message_toolbar/attachment_dialog.rs
  3. 16
      src/session/view/media_viewer.rs

42
src/components/media/content_viewer.rs

@ -146,24 +146,28 @@ mod imp {
}
/// View the given file.
pub(super) async fn view_file(&self, file: gio::File) {
pub(super) async fn view_file(&self, file: gio::File, content_type: Option<ContentType>) {
self.set_visible_child("loading");
let file_info = file
.query_info_future(
gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
gio::FileQueryInfoFlags::NONE,
glib::Priority::DEFAULT,
)
.await
.ok();
let content_type: ContentType = file_info
.as_ref()
.and_then(gio::FileInfo::content_type)
.and_then(|content_type| gio::content_type_get_mime_type(&content_type))
.and_then(|mime| mime.split('/').next().map(Into::into))
.unwrap_or_default();
let content_type = if let Some(content_type) = content_type {
content_type
} else {
let file_info = file
.query_info_future(
gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
gio::FileQueryInfoFlags::NONE,
glib::Priority::DEFAULT,
)
.await
.ok();
file_info
.as_ref()
.and_then(gio::FileInfo::content_type)
.and_then(|content_type| gio::content_type_get_mime_type(&content_type))
.and_then(|mime| mime.split('/').next().map(Into::into))
.unwrap_or_default()
};
match content_type {
ContentType::Image => {
@ -306,8 +310,10 @@ impl MediaContentViewer {
}
/// View the given file.
pub(crate) async fn view_file(&self, file: gio::File) {
self.imp().view_file(file).await;
///
/// If the content type is not provided, it will be guessed from the file.
pub(crate) async fn view_file(&self, file: gio::File, content_type: Option<ContentType>) {
self.imp().view_file(file, content_type).await;
}
/// View the given location as a geo URI.

2
src/session/view/content/room_history/message_toolbar/attachment_dialog.rs

@ -114,7 +114,7 @@ impl AttachmentDialog {
#[weak]
imp,
async move {
imp.media.view_file(file).await;
imp.media.view_file(file, None).await;
imp.set_loading(false);
}
));

16
src/session/view/media_viewer.rs

@ -369,22 +369,18 @@ mod imp {
return;
};
let client = session.client();
let is_video = matches!(message, VisualMediaMessage::Video(_));
let content_type = match &message {
VisualMediaMessage::Image(_) | VisualMediaMessage::Sticker(_) => ContentType::Image,
VisualMediaMessage::Video(_) => ContentType::Video,
};
let client = session.client();
match message.into_tmp_file(&client).await {
Ok(file) => {
self.media.view_file(file).await;
self.media.view_file(file, Some(content_type)).await;
}
Err(error) => {
warn!("Could not retrieve media file: {error}");
let content_type = if is_video {
ContentType::Video
} else {
ContentType::Image
};
self.media.show_fallback(content_type);
}
}

Loading…
Cancel
Save