From ec1879d212fa8d6e5f8590486e94c72abfcb75a5 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Fri, 24 Jan 2025 11:35:08 +0200 Subject: [PATCH] Populate image/audio attachment `body` with a filename, not with text Various clients (including newer versions of Element Web), do not like it when the `body` field of the attachment is not a file name. For images, a preview may not be shown and downloading the attachment may suggest that the whole long text is used as a filename (which is odd). There is value (improved accessibility, etc.) in adding better descriptions (especially to generated images), but given that it's currently problematic, I'm getting rid of it. It's better and safer if we stick to using filenames. --- src/controller/image/generation.rs | 5 +++-- src/controller/utils/mime.rs | 16 ++++++++++++++++ src/controller/utils/mod.rs | 1 + src/controller/utils/text_to_speech.rs | 3 ++- src/strings/text_to_speech.rs | 4 ---- 5 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/controller/utils/mime.rs diff --git a/src/controller/image/generation.rs b/src/controller/image/generation.rs index 823593b..9bfb672 100644 --- a/src/controller/image/generation.rs +++ b/src/controller/image/generation.rs @@ -10,6 +10,7 @@ use crate::conversation::create_llm_conversation_for_matrix_thread; use crate::conversation::matrix::MatrixMessageProcessingParams; use crate::strings; use crate::{entity::MessageContext, Bot}; +use crate::controller::utils::mime::get_file_extension; // We may make this configurable (per room, etc.) in the future, but for now it's hardcoded. const STICKER_SIZE: &str = "256x256"; @@ -81,7 +82,7 @@ pub async fn handle_image( .await; } - let attachment_body_text = format!("Generated image based on: {}", actual_prompt); + let attachment_body_text = format!("generated-image.{}", get_file_extension(&response.mime_type)); let mut event_content = matrix_link .media() @@ -157,7 +158,7 @@ pub async fn handle_sticker( .instrument(span) .await?; - let attachment_body_text = format!("Generated sticker image based on: {}", original_prompt); + let attachment_body_text = format!("generated-sticker.{}", get_file_extension(&response.mime_type)); let mut event_content = matrix_link .media() diff --git a/src/controller/utils/mime.rs b/src/controller/utils/mime.rs new file mode 100644 index 0000000..8c9a8e7 --- /dev/null +++ b/src/controller/utils/mime.rs @@ -0,0 +1,16 @@ +use mxlink::mime; + +pub fn get_file_extension(mime_type: &mime::Mime) -> String { + match (mime_type.type_(), mime_type.subtype()) { + (mime::AUDIO, mime::BASIC) => "au", + (mime::AUDIO, mime::MPEG) => "mp3", + (mime::AUDIO, mime::MP4) => "m4a", + (mime::AUDIO, mime::OGG) => "ogg", + (mime::IMAGE, mime::BMP) => "bmp", + (mime::IMAGE, mime::GIF) => "gif", + (mime::IMAGE, mime::JPEG) => "jpg", + (mime::IMAGE, mime::PNG) => "png", + (mime::IMAGE, mime::SVG) => "svg", + _ => "bin", + }.to_string() +} diff --git a/src/controller/utils/mod.rs b/src/controller/utils/mod.rs index 9f493c1..f7b9a25 100644 --- a/src/controller/utils/mod.rs +++ b/src/controller/utils/mod.rs @@ -7,6 +7,7 @@ use crate::{ pub mod agent; pub mod text_to_speech; +pub(super) mod mime; pub async fn get_text_body_or_complain<'a>( bot: &Bot, diff --git a/src/controller/utils/text_to_speech.rs b/src/controller/utils/text_to_speech.rs index e55f81e..0178b66 100644 --- a/src/controller/utils/text_to_speech.rs +++ b/src/controller/utils/text_to_speech.rs @@ -3,6 +3,7 @@ use mxlink::{MatrixLink, MessageResponseType}; use tracing::Instrument; +use crate::controller::utils::mime::get_file_extension; use crate::{ agent::{provider::TextToSpeechParams, AgentInstance, AgentPurpose, ControllerTrait}, entity::MessageContext, @@ -117,7 +118,7 @@ async fn do_generate_and_send_tts_for_message( } }; - let attachment_body_text = strings::text_to_speech::alternate_body_text(); + let attachment_body_text = format!("generated-speech.{}", get_file_extension(&text_to_speech_result.mime_type)); let event_content = matrix_link .media() diff --git a/src/strings/text_to_speech.rs b/src/strings/text_to_speech.rs index e9f9ee7..19e9f2f 100644 --- a/src/strings/text_to_speech.rs +++ b/src/strings/text_to_speech.rs @@ -1,7 +1,3 @@ -pub fn alternate_body_text() -> String { - "Generated speech from text".to_owned() -} - pub fn redaction_reason_done() -> &'static str { "Done with speech-to-text" }