mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-16 12:47:55 +08:00
feat: add downloads directory argument for attachment download
Refs: #559
This commit is contained in:
@@ -10,8 +10,8 @@ use tracing::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
account::arg::name::AccountNameFlag, config::TomlConfig, envelope::arg::ids::EnvelopeIdsArgs,
|
||||
folder::arg::name::FolderNameOptionalFlag,
|
||||
account::arg::name::AccountNameFlag, config::TomlConfig, dir_parser,
|
||||
envelope::arg::ids::EnvelopeIdsArgs, folder::arg::name::FolderNameOptionalFlag,
|
||||
};
|
||||
|
||||
/// Download all attachments found in the given message.
|
||||
@@ -28,6 +28,13 @@ pub struct AttachmentDownloadCommand {
|
||||
|
||||
#[command(flatten)]
|
||||
pub account: AccountNameFlag,
|
||||
|
||||
/// Override the download directory.
|
||||
///
|
||||
/// If omitted, it uses the downloads directory from the config,
|
||||
/// or `XDG_DOWNLOAD_DIR`.
|
||||
#[arg(short = 'd', long, value_name = "PATH", value_parser = dir_parser)]
|
||||
pub downloads_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl AttachmentDownloadCommand {
|
||||
@@ -85,7 +92,8 @@ impl AttachmentDownloadCommand {
|
||||
.filename
|
||||
.unwrap_or_else(|| Uuid::new_v4().to_string())
|
||||
.into();
|
||||
let filepath = account_config.get_download_file_path(&filename)?;
|
||||
let filepath = account_config
|
||||
.get_download_file_path(self.downloads_dir.as_deref(), &filename)?;
|
||||
printer.log(format!("Downloading {:?}…\n", filepath))?;
|
||||
fs::write(&filepath, &attachment.body)
|
||||
.with_context(|| format!("cannot save attachment at {filepath:?}"))?;
|
||||
|
||||
+14
@@ -6,5 +6,19 @@ pub mod email;
|
||||
pub mod folder;
|
||||
pub mod manual;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use shellexpand_utils::{canonicalize, expand};
|
||||
|
||||
#[doc(inline)]
|
||||
pub use crate::email::{envelope, flag, message};
|
||||
|
||||
/// Parse the given [`str`] as [`PathBuf`].
|
||||
///
|
||||
/// The path is first shell expanded, then canonicalized (if
|
||||
/// applicable).
|
||||
fn dir_parser(path: &str) -> Result<PathBuf, String> {
|
||||
expand::try_path(path)
|
||||
.map(canonicalize::path)
|
||||
.map_err(|err| err.to_string())
|
||||
}
|
||||
|
||||
+1
-12
@@ -4,10 +4,9 @@ use clap::{CommandFactory, Parser};
|
||||
use clap_mangen::Man;
|
||||
use color_eyre::Result;
|
||||
use pimalaya_tui::terminal::cli::printer::Printer;
|
||||
use shellexpand_utils::{canonicalize, expand};
|
||||
use tracing::info;
|
||||
|
||||
use crate::cli::Cli;
|
||||
use crate::{cli::Cli, dir_parser};
|
||||
|
||||
/// Generate manual pages to the given directory.
|
||||
///
|
||||
@@ -61,13 +60,3 @@ impl ManualGenerateCommand {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the given [`str`] as [`PathBuf`].
|
||||
///
|
||||
/// The path is first shell expanded, then canonicalized (if
|
||||
/// applicable).
|
||||
fn dir_parser(path: &str) -> Result<PathBuf, String> {
|
||||
expand::try_path(path)
|
||||
.map(canonicalize::path)
|
||||
.map_err(|err| err.to_string())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user