mimefactory: resultify is_file_size_okay

This commit is contained in:
link2xt
2021-04-15 03:20:27 +03:00
parent 9ecb6d9b15
commit ac54301923

View File

@@ -1,6 +1,6 @@
use std::convert::TryInto; use std::convert::TryInto;
use anyhow::{bail, ensure, format_err, Error}; use anyhow::{bail, ensure, format_err, Result};
use async_std::prelude::*; use async_std::prelude::*;
use chrono::TimeZone; use chrono::TimeZone;
use lettre_email::{mime, Address, Header, MimeMultipartType, PartBuilder}; use lettre_email::{mime, Address, Header, MimeMultipartType, PartBuilder};
@@ -90,7 +90,7 @@ impl<'a> MimeFactory<'a> {
context: &Context, context: &Context,
msg: &'a Message, msg: &'a Message,
attach_selfavatar: bool, attach_selfavatar: bool,
) -> Result<MimeFactory<'a>, Error> { ) -> Result<MimeFactory<'a>> {
let chat = Chat::load_from_db(context, msg.chat_id).await?; let chat = Chat::load_from_db(context, msg.chat_id).await?;
let from_addr = context let from_addr = context
@@ -178,7 +178,7 @@ impl<'a> MimeFactory<'a> {
context: &Context, context: &Context,
msg: &'a Message, msg: &'a Message,
additional_msg_ids: Vec<String>, additional_msg_ids: Vec<String>,
) -> Result<MimeFactory<'a>, Error> { ) -> Result<MimeFactory<'a>> {
ensure!(!msg.chat_id.is_special(), "Invalid chat id"); ensure!(!msg.chat_id.is_special(), "Invalid chat id");
let contact = Contact::load_from_db(context, msg.from_id).await?; let contact = Contact::load_from_db(context, msg.from_id).await?;
@@ -222,7 +222,7 @@ impl<'a> MimeFactory<'a> {
async fn peerstates_for_recipients( async fn peerstates_for_recipients(
&self, &self,
context: &Context, context: &Context,
) -> Result<Vec<(Option<Peerstate>, &str)>, Error> { ) -> Result<Vec<(Option<Peerstate>, &str)>> {
let self_addr = context let self_addr = context
.get_config(Config::ConfiguredAddr) .get_config(Config::ConfiguredAddr)
.await? .await?
@@ -302,7 +302,7 @@ impl<'a> MimeFactory<'a> {
} }
} }
async fn should_do_gossip(&self, context: &Context) -> Result<bool, Error> { async fn should_do_gossip(&self, context: &Context) -> Result<bool> {
match &self.loaded { match &self.loaded {
Loaded::Message { chat } => { Loaded::Message { chat } => {
// beside key- and member-changes, force re-gossip every 48 hours // beside key- and member-changes, force re-gossip every 48 hours
@@ -401,7 +401,7 @@ impl<'a> MimeFactory<'a> {
.collect() .collect()
} }
pub async fn render(mut self, context: &Context) -> Result<RenderedEmail, Error> { pub async fn render(mut self, context: &Context) -> Result<RenderedEmail> {
// Headers that are encrypted // Headers that are encrypted
// - Chat-*, except Chat-Version // - Chat-*, except Chat-Version
// - Secure-Join* // - Secure-Join*
@@ -673,7 +673,7 @@ impl<'a> MimeFactory<'a> {
Some(part) Some(part)
} }
async fn get_location_kml_part(&mut self, context: &Context) -> Result<PartBuilder, Error> { async fn get_location_kml_part(&mut self, context: &Context) -> Result<PartBuilder> {
let (kml_content, last_added_location_id) = let (kml_content, last_added_location_id) =
location::get_kml(context, self.msg.chat_id).await?; location::get_kml(context, self.msg.chat_id).await?;
let part = PartBuilder::new() let part = PartBuilder::new()
@@ -701,7 +701,7 @@ impl<'a> MimeFactory<'a> {
protected_headers: &mut Vec<Header>, protected_headers: &mut Vec<Header>,
unprotected_headers: &mut Vec<Header>, unprotected_headers: &mut Vec<Header>,
grpimage: &Option<String>, grpimage: &Option<String>,
) -> Result<(PartBuilder, Vec<PartBuilder>), Error> { ) -> Result<(PartBuilder, Vec<PartBuilder>)> {
let chat = match &self.loaded { let chat = match &self.loaded {
Loaded::Message { chat } => chat, Loaded::Message { chat } => chat,
Loaded::Mdn { .. } => bail!("Attempt to render MDN as a message"), Loaded::Mdn { .. } => bail!("Attempt to render MDN as a message"),
@@ -977,7 +977,7 @@ impl<'a> MimeFactory<'a> {
// add attachment part // add attachment part
if chat::msgtype_has_file(self.msg.viewtype) { if chat::msgtype_has_file(self.msg.viewtype) {
if !is_file_size_okay(context, self.msg).await { if !is_file_size_okay(context, self.msg).await? {
bail!( bail!(
"Message exceeds the recommended {} MB.", "Message exceeds the recommended {} MB.",
RECOMMENDED_FILE_SIZE / 1_000_000, RECOMMENDED_FILE_SIZE / 1_000_000,
@@ -1022,7 +1022,7 @@ impl<'a> MimeFactory<'a> {
} }
/// Render an MDN /// Render an MDN
async fn render_mdn(&mut self, context: &Context) -> Result<PartBuilder, Error> { async fn render_mdn(&mut self, context: &Context) -> Result<PartBuilder> {
// RFC 6522, this also requires the `report-type` parameter which is equal // RFC 6522, this also requires the `report-type` parameter which is equal
// to the MIME subtype of the second body part of the multipart/report // to the MIME subtype of the second body part of the multipart/report
// //
@@ -1120,7 +1120,7 @@ async fn build_body_file(
context: &Context, context: &Context,
msg: &Message, msg: &Message,
base_name: &str, base_name: &str,
) -> Result<(PartBuilder, String), Error> { ) -> Result<(PartBuilder, String)> {
let blob = msg let blob = msg
.param .param
.get_blob(Param::File, context, true) .get_blob(Param::File, context, true)
@@ -1194,7 +1194,7 @@ async fn build_body_file(
Ok((mail, filename_to_send)) Ok((mail, filename_to_send))
} }
fn build_selfavatar_file(context: &Context, path: &str) -> Result<(PartBuilder, String), Error> { fn build_selfavatar_file(context: &Context, path: &str) -> Result<(PartBuilder, String)> {
let blob = BlobObject::from_path(context, path)?; let blob = BlobObject::from_path(context, path)?;
let filename_to_send = match blob.suffix() { let filename_to_send = match blob.suffix() {
Some(suffix) => format!("avatar.{}", suffix), Some(suffix) => format!("avatar.{}", suffix),
@@ -1226,13 +1226,13 @@ fn recipients_contain_addr(recipients: &[(String, String)], addr: &str) -> bool
.any(|(_, cur)| cur.to_lowercase() == addr_lc) .any(|(_, cur)| cur.to_lowercase() == addr_lc)
} }
async fn is_file_size_okay(context: &Context, msg: &Message) -> bool { async fn is_file_size_okay(context: &Context, msg: &Message) -> Result<bool> {
match msg.param.get_path(Param::File, context).unwrap_or(None) { match msg.param.get_path(Param::File, context)? {
Some(path) => { Some(path) => {
let bytes = dc_get_filebytes(context, &path).await; let bytes = dc_get_filebytes(context, &path).await;
bytes <= UPPER_LIMIT_FILE_SIZE Ok(bytes <= UPPER_LIMIT_FILE_SIZE)
} }
None => false, None => Ok(false),
} }
} }