review attachment command

This commit is contained in:
Clément DOUIN 2021-09-19 10:55:15 +02:00
parent 474fae06ee
commit 2bcad3ff87
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
2 changed files with 10 additions and 14 deletions

View file

@ -9,6 +9,7 @@ use crate::domain::msg;
/// Message attachment subcommands.
pub(crate) fn subcmds<'a>() -> Vec<App<'a, 'a>> {
vec![SubCommand::with_name("attachments")
.aliases(&["attachment", "att", "a"])
.about("Downloads all message attachments")
.arg(msg::arg::uid_arg())]
}

View file

@ -116,7 +116,8 @@ fn msg_interaction<ImapService: ImapServiceInterface, SmtpService: SmtpServiceIn
Ok(true)
}
pub fn attachments<ImapService: ImapServiceInterface>(
/// Download all attachments from the given message UID to the user account downloads directory.
pub fn attachments<OutputService: OutputServiceInterface, ImapService: ImapServiceInterface>(
uid: &str,
account: &Account,
output: &OutputService,
@ -127,27 +128,21 @@ pub fn attachments<ImapService: ImapServiceInterface>(
debug!(
"{} attachment(s) found for message {}",
&attachments.len(),
&uid
attachments.len(),
uid
);
// Iterate through all attachments and download them to the download
// directory of the account.
for attachment in &attachments {
let filepath = account.downloads_dir.join(&attachment.filename);
debug!("downloading {}…", &attachment.filename);
debug!("downloading {}…", attachment.filename);
fs::write(&filepath, &attachment.body_raw)
.context(format!("cannot save attachment {:?}", filepath))?;
.context(format!("cannot download attachment {:?}", filepath))?;
}
debug!(
"{} attachment(s) successfully downloaded",
&attachments.len()
);
output.print(format!(
"{} attachment(s) successfully downloaded",
&attachments.len()
"{} attachment(s) successfully downloaded to {:?}",
attachments.len(),
account.downloads_dir
))?;
imap.logout()?;