Resultify Message::get_filebytes()

This commit is contained in:
link2xt
2023-01-06 19:22:57 +00:00
parent 087b4289e5
commit 58ba107d01
8 changed files with 18 additions and 16 deletions

View File

@@ -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(())
}

View File

@@ -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<Option<u64>> {
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<String> {
}
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);
}

View File

@@ -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<bool> {
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),

View File

@@ -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);

View File

@@ -364,12 +364,10 @@ pub(crate) fn get_abs_path(context: &Context, path: impl AsRef<Path>) -> PathBuf
}
}
pub(crate) async fn get_filebytes(context: &Context, path: impl AsRef<Path>) -> u64 {
pub(crate) async fn get_filebytes(context: &Context, path: impl AsRef<Path>) -> Result<u64> {
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<Path>) -> 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()