diff --git a/CHANGELOG.md b/CHANGELOG.md index 37715b863..b28312450 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - jsonrpc: add python API for webxdc updates #3872 - jsonrpc: add fresh message count to ChatListItemFetchResult::ArchiveLink - Add ffi functions to retrieve `verified by` information #3786 +- resultify `Message::get_filebytes()` #3925 ### Fixes - Do not add an error if the message is encrypted but not signed #3860 diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 764f1acad..7de6e4cd8 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -3309,6 +3309,8 @@ pub unsafe extern "C" fn dc_msg_get_filebytes(msg: *mut dc_msg_t) -> u64 { let ctx = &*ffi_msg.context; block_on(ffi_msg.message.get_filebytes(ctx)) + .unwrap_or_log_default(ctx, "Cannot get file size") + .unwrap_or_default() } #[no_mangle] diff --git a/deltachat-jsonrpc/src/api/types/message.rs b/deltachat-jsonrpc/src/api/types/message.rs index c7e9cfe8d..0f35f0c6f 100644 --- a/deltachat-jsonrpc/src/api/types/message.rs +++ b/deltachat-jsonrpc/src/api/types/message.rs @@ -105,7 +105,7 @@ impl MessageObject { let sender_contact = Contact::load_from_db(context, message.get_from_id()).await?; let sender = ContactObject::try_from_dc_contact(context, sender_contact).await?; - let file_bytes = message.get_filebytes(context).await; + let file_bytes = message.get_filebytes(context).await?.unwrap_or_default(); let override_sender_name = message.get_override_sender_name(); let webxdc_info = if message.get_viewtype() == Viewtype::Webxdc { diff --git a/src/chat.rs b/src/chat.rs index d22cec77d..618319cb1 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -5202,7 +5202,7 @@ mod tests { assert_eq!(msg.get_filename(), Some(filename.to_string())); assert_eq!(msg.get_width(), w); assert_eq!(msg.get_height(), h); - assert!(msg.get_filebytes(&bob).await > 250); + assert!(msg.get_filebytes(&bob).await?.unwrap() > 250); Ok(()) } diff --git a/src/message.rs b/src/message.rs index 5535666bd..b1581137f 100644 --- a/src/message.rs +++ b/src/message.rs @@ -494,11 +494,12 @@ impl Message { .map(|name| name.to_string_lossy().to_string()) } - pub async fn get_filebytes(&self, context: &Context) -> u64 { - match self.param.get_path(Param::File, context) { - Ok(Some(path)) => get_filebytes(context, &path).await, - Ok(None) => 0, - Err(_) => 0, + /// Returns the size of the file in bytes, if applicable. + pub async fn get_filebytes(&self, context: &Context) -> Result> { + if let Some(path) = self.param.get_path(Param::File, context)? { + Ok(Some(get_filebytes(context, &path).await?)) + } else { + Ok(None) } } @@ -1102,7 +1103,7 @@ pub async fn get_msg_info(context: &Context, msg_id: MsgId) -> Result { } if let Some(path) = msg.get_file(context) { - let bytes = get_filebytes(context, &path).await; + let bytes = get_filebytes(context, &path).await?; ret += &format!("\nFile: {}, {}, bytes\n", path.display(), bytes); } diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 43f3e5886..5d7d51fe8 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -1432,7 +1432,7 @@ fn recipients_contain_addr(recipients: &[(String, String)], addr: &str) -> bool async fn is_file_size_okay(context: &Context, msg: &Message) -> Result { match msg.param.get_path(Param::File, context)? { Some(path) => { - let bytes = get_filebytes(context, &path).await; + let bytes = get_filebytes(context, &path).await?; Ok(bytes <= UPPER_LIMIT_FILE_SIZE) } None => Ok(false), diff --git a/src/mimeparser.rs b/src/mimeparser.rs index e7a2ea40e..c83709673 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -3145,7 +3145,7 @@ On 2020-10-25, Bob wrote: assert_eq!(msg.is_dc_message, MessengerMessage::No); assert_eq!(msg.chat_blocked, Blocked::Request); assert_eq!(msg.state, MessageState::InFresh); - assert_eq!(msg.get_filebytes(&t).await, 2115); + assert_eq!(msg.get_filebytes(&t).await.unwrap().unwrap(), 2115); assert!(msg.get_file(&t).is_some()); assert_eq!(msg.get_filename().unwrap(), "avatar64x64.png"); assert_eq!(msg.get_width(), 64); diff --git a/src/tools.rs b/src/tools.rs index b02ed2fea..e9c34f5e1 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -364,12 +364,10 @@ pub(crate) fn get_abs_path(context: &Context, path: impl AsRef) -> PathBuf } } -pub(crate) async fn get_filebytes(context: &Context, path: impl AsRef) -> u64 { +pub(crate) async fn get_filebytes(context: &Context, path: impl AsRef) -> Result { let path_abs = get_abs_path(context, &path); - match fs::metadata(&path_abs).await { - Ok(meta) => meta.len(), - Err(_err) => 0, - } + let meta = fs::metadata(&path_abs).await?; + Ok(meta.len()) } pub(crate) async fn delete_file(context: &Context, path: impl AsRef) -> bool { @@ -1055,7 +1053,7 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true"; .is_ok()); assert!(file_exist!(context, "$BLOBDIR/foobar")); assert!(!file_exist!(context, "$BLOBDIR/foobarx")); - assert_eq!(get_filebytes(context, "$BLOBDIR/foobar").await, 7); + assert_eq!(get_filebytes(context, "$BLOBDIR/foobar").await.unwrap(), 7); let abs_path = context .get_blobdir()