mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 00:59:02 +03:00
feat: add --compress option (#319)
This commit is contained in:
69
src/args.rs
69
src/args.rs
@@ -1,6 +1,7 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use clap::builder::PossibleValuesParser;
|
||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
||||
use async_zip::Compression;
|
||||
use clap::builder::{PossibleValue, PossibleValuesParser};
|
||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command, ValueEnum};
|
||||
use clap_complete::{generate, Generator, Shell};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use smart_default::SmartDefault;
|
||||
@@ -196,6 +197,15 @@ pub fn build_cli() -> Command {
|
||||
.value_name("format")
|
||||
.help("Customize http log format"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("compress")
|
||||
.env("DUFS_COMPRESS")
|
||||
.hide_env(true)
|
||||
.value_parser(clap::builder::EnumValueParser::<Compress>::new())
|
||||
.long("compress")
|
||||
.value_name("level")
|
||||
.help("Set zip compress level [default: low]")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("completions")
|
||||
.long("completions")
|
||||
@@ -270,6 +280,7 @@ pub struct Args {
|
||||
#[serde(deserialize_with = "deserialize_log_http")]
|
||||
#[serde(rename = "log-format")]
|
||||
pub http_logger: HttpLogger,
|
||||
pub compress: Compress,
|
||||
pub tls_cert: Option<PathBuf>,
|
||||
pub tls_key: Option<PathBuf>,
|
||||
}
|
||||
@@ -369,10 +380,6 @@ impl Args {
|
||||
args.render_spa = matches.get_flag("render-spa");
|
||||
}
|
||||
|
||||
if let Some(log_format) = matches.get_one::<String>("log-format") {
|
||||
args.http_logger = log_format.parse()?;
|
||||
}
|
||||
|
||||
if let Some(assets_path) = matches.get_one::<PathBuf>("assets") {
|
||||
args.assets = Some(assets_path.clone());
|
||||
}
|
||||
@@ -381,6 +388,14 @@ impl Args {
|
||||
args.assets = Some(Args::sanitize_assets_path(assets_path)?);
|
||||
}
|
||||
|
||||
if let Some(log_format) = matches.get_one::<String>("log-format") {
|
||||
args.http_logger = log_format.parse()?;
|
||||
}
|
||||
|
||||
if let Some(compress) = matches.get_one::<Compress>("compress") {
|
||||
args.compress = *compress;
|
||||
}
|
||||
|
||||
#[cfg(feature = "tls")]
|
||||
{
|
||||
if let Some(tls_cert) = matches.get_one::<PathBuf>("tls-cert") {
|
||||
@@ -403,6 +418,7 @@ impl Args {
|
||||
args.tls_cert = None;
|
||||
args.tls_key = None;
|
||||
}
|
||||
println!("{args:?}");
|
||||
|
||||
Ok(args)
|
||||
}
|
||||
@@ -461,6 +477,47 @@ impl BindAddr {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Compress {
|
||||
None,
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
}
|
||||
|
||||
impl Default for Compress {
|
||||
fn default() -> Self {
|
||||
Self::Low
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueEnum for Compress {
|
||||
fn value_variants<'a>() -> &'a [Self] {
|
||||
&[Self::None, Self::Low, Self::Medium, Self::High]
|
||||
}
|
||||
|
||||
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
|
||||
Some(match self {
|
||||
Compress::None => PossibleValue::new("none"),
|
||||
Compress::Low => PossibleValue::new("low"),
|
||||
Compress::Medium => PossibleValue::new("medium"),
|
||||
Compress::High => PossibleValue::new("high"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Compress {
|
||||
pub fn to_compression(self) -> Compression {
|
||||
match self {
|
||||
Compress::None => Compression::Stored,
|
||||
Compress::Low => Compression::Deflate,
|
||||
Compress::Medium => Compression::Bz,
|
||||
Compress::High => Compression::Xz,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_bind_addrs<'de, D>(deserializer: D) -> Result<Vec<BindAddr>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
|
||||
@@ -581,8 +581,18 @@ impl Server {
|
||||
let path = path.to_owned();
|
||||
let hidden = self.args.hidden.clone();
|
||||
let running = self.running.clone();
|
||||
let compression = self.args.compress.to_compression();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = zip_dir(&mut writer, &path, access_paths, &hidden, running).await {
|
||||
if let Err(e) = zip_dir(
|
||||
&mut writer,
|
||||
&path,
|
||||
access_paths,
|
||||
&hidden,
|
||||
compression,
|
||||
running,
|
||||
)
|
||||
.await
|
||||
{
|
||||
error!("Failed to zip {}, {}", path.display(), e);
|
||||
}
|
||||
});
|
||||
@@ -1422,6 +1432,7 @@ async fn zip_dir<W: AsyncWrite + Unpin>(
|
||||
dir: &Path,
|
||||
access_paths: AccessPaths,
|
||||
hidden: &[String],
|
||||
compression: Compression,
|
||||
running: Arc<AtomicBool>,
|
||||
) -> Result<()> {
|
||||
let mut writer = ZipFileWriter::with_tokio(writer);
|
||||
@@ -1475,7 +1486,7 @@ async fn zip_dir<W: AsyncWrite + Unpin>(
|
||||
None => continue,
|
||||
};
|
||||
let (datetime, mode) = get_file_mtime_and_mode(&zip_path).await?;
|
||||
let builder = ZipEntryBuilder::new(filename.into(), Compression::Deflate)
|
||||
let builder = ZipEntryBuilder::new(filename.into(), compression)
|
||||
.unix_permissions(mode)
|
||||
.last_modification_date(ZipDateTime::from_chrono(&datetime));
|
||||
let mut file = File::open(&zip_path).await?;
|
||||
|
||||
Reference in New Issue
Block a user