fixup this PR with tests, and returning None from get_filemime

This commit is contained in:
holger krekel
2019-09-26 00:33:58 +02:00
parent 53b5cbc12a
commit 02b73207f9
3 changed files with 16 additions and 9 deletions

View File

@@ -2362,7 +2362,11 @@ pub unsafe extern "C" fn dc_msg_get_filemime(msg: *mut dc_msg_t) -> *mut libc::c
return dc_strdup(ptr::null());
}
let ffi_msg = &*msg;
ffi_msg.message.get_filemime().strdup()
if let Some(x) = ffi_msg.message.get_filemime() {
x.strdup()
} else {
return dc_strdup(ptr::null());
}
}
#[no_mangle]

View File

@@ -93,8 +93,9 @@ class TestOfflineContact:
ac1 = acfactory.get_configured_offline_account()
contact1 = ac1.create_contact(email="some1@example.com", name="some1")
chat = ac1.create_chat_by_contact(contact1)
chat.send_text("one messae")
msg = chat.send_text("one messae")
assert not ac1.delete_contact(contact1)
assert not msg.filemime
class TestOfflineChat:

View File

@@ -143,17 +143,18 @@ impl Message {
}
}
pub fn get_filemime(&self) -> String {
pub fn get_filemime(&self) -> Option<String> {
if let Some(m) = self.param.get(Param::MimeType) {
return m.to_string();
return Some(m.to_string());
} else if let Some(file) = self.param.get(Param::File) {
if let Some((_, mime)) = guess_msgtype_from_suffix(Path::new(file)) {
return mime.to_string();
return Some(mime.to_string());
}
return "application/octet-stream".to_string()
// we have a file but no mimetype, let's use a generic one
return Some("application/octet-stream".to_string());
}
"".to_string()
// no mimetype and no file
None
}
pub fn get_file(&self, context: &Context) -> Option<PathBuf> {
@@ -644,7 +645,7 @@ pub fn get_msg_info(context: &Context, msg_id: u32) -> String {
ret += "Type: ";
ret += &format!("{}", msg.type_0);
ret += "\n";
ret += &format!("Mimetype: {}\n", &msg.get_filemime());
ret += &format!("Mimetype: {}\n", &msg.get_filemime().unwrap_or_default());
}
let w = msg.param.get_int(Param::Width).unwrap_or_default();
let h = msg.param.get_int(Param::Height).unwrap_or_default();
@@ -1155,5 +1156,6 @@ mod tests {
let msg_id = chat::prepare_msg(ctx, chat, &mut msg).unwrap();
let _msg2 = Message::load_from_db(ctx, msg_id).unwrap();
assert_eq!(_msg2.get_filemime(), None);
}
}