diff --git a/src/export_chat.rs b/src/export_chat.rs index 3fdfefc0d..f1b0f9e00 100644 --- a/src/export_chat.rs +++ b/src/export_chat.rs @@ -1,6 +1,6 @@ // use crate::dc_tools::*; use crate::chat::*; -use crate::constants::DC_CONTACT_ID_SELF; +use crate::constants::{Viewtype, DC_CONTACT_ID_SELF}; use crate::contact::*; use crate::context::Context; use crate::error::Error; @@ -128,7 +128,7 @@ pub fn export_chat(context: &Context, chat_id: ChatId) -> ExportChatResult { let mut html_messages: Vec = Vec::new(); for message in messages { if let Ok(msg) = message { - html_messages.push(message_to_html(&chat_authors, msg)); + html_messages.push(message_to_html(&chat_authors, msg, context)); } else { html_messages.push(format!( r#"
  • @@ -151,7 +151,7 @@ pub fn export_chat(context: &Context, chat_id: ChatId) -> ExportChatResult { Some(img) => { let path = img .file_name() - .unwrap_or_else(|| std::ffi::OsStr::new("")) + .unwrap_or_else(|| std::ffi::OsStr::new("")) .to_str() .unwrap() .to_owned(); @@ -198,7 +198,11 @@ pub fn export_chat(context: &Context, chat_id: ChatId) -> ExportChatResult { } } -fn message_to_html(author_cache: &HashMap, message: Message) -> String { +fn message_to_html( + author_cache: &HashMap, + message: Message, + context: &Context, +) -> String { let author: &ContactInfo = { if let Some(c) = author_cache.get(&message.get_from_id()) { c @@ -235,7 +239,62 @@ fn message_to_html(author_cache: &HashMap, message: Message) - // save and refernce message source code somehow? - //todo support images / voice message / attachments + let has_text = message.get_text().is_some() && !message.get_text().unwrap().is_empty(); + + let attachment = match message.get_file(context) { + None => "".to_owned(), + Some(file) => { + let modifier_class = if has_text { "content-below" } else { "" }; + let filename = file + .file_name() + .unwrap_or_else(|| std::ffi::OsStr::new("")) + .to_str() + .unwrap() + .to_owned(); + match message.get_viewtype() { + Viewtype::Audio => { + format!("", modifier_class ,filename) + }, + Viewtype::Gif | Viewtype::Image | Viewtype::Sticker => { + format!(" \ + \ + ", modifier_class=modifier_class, filename=filename) + }, + Viewtype::Video => { + format!(" \ + ", modifier_class=modifier_class, filename=filename) + }, + _ => { + format!("
    \ +
    \ +
    \ + {extension} \ +
    \ +
    \ +
    \ + {filename}\ +
    {filesize}
    \ +
    \ +
    ", + modifier_class=modifier_class, + filename=filename, + filesize=message.get_filebytes(&context) /* todo human readable file size*/, + extension=file.extension().unwrap_or_else(|| std::ffi::OsStr::new("")).to_str().unwrap().to_owned()) + } + } + } + }; format!( "
  • \ @@ -244,10 +303,11 @@ fn message_to_html(author_cache: &HashMap, message: Message) -
    \ {author_name}\
    \ + {attachment}
    \ {content}\
    \ -
    \ +