mimefactory: use async_std instead of std

Read files asynchronously to avoid blocking threads.
This commit is contained in:
link2xt
2022-06-12 18:38:10 +00:00
parent 1ae67c4549
commit 776a3ecd1f

View File

@@ -3,6 +3,7 @@
use std::convert::TryInto;
use anyhow::{bail, ensure, Context as _, Result};
use async_std::fs;
use chrono::TimeZone;
use lettre_email::{mime, Address, Header, MimeMultipartType, PartBuilder};
@@ -1183,7 +1184,7 @@ impl<'a> MimeFactory<'a> {
if self.attach_selfavatar {
match context.get_config(Config::Selfavatar).await? {
Some(path) => match build_selfavatar_file(context, &path) {
Some(path) => match build_selfavatar_file(context, &path).await {
Ok(avatar) => headers.hidden.push(Header::new(
"Chat-User-Avatar".into(),
format!("base64:{}", avatar),
@@ -1366,7 +1367,7 @@ async fn build_body_file(
maybe_encode_words(&filename_to_send)
);
let body = std::fs::read(blob.to_abs_path())?;
let body = fs::read(blob.to_abs_path()).await?;
let encoded_body = wrapped_base64_encode(&body);
let mail = PartBuilder::new()
@@ -1378,9 +1379,9 @@ async fn build_body_file(
Ok((mail, filename_to_send))
}
fn build_selfavatar_file(context: &Context, path: &str) -> Result<String> {
async fn build_selfavatar_file(context: &Context, path: &str) -> Result<String> {
let blob = BlobObject::from_path(context, path.as_ref())?;
let body = std::fs::read(blob.to_abs_path())?;
let body = fs::read(blob.to_abs_path()).await?;
let encoded_body = wrapped_base64_encode(&body);
Ok(encoded_body)
}