diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 46 |
3 files changed, 35 insertions, 21 deletions
diff --git a/src/args.rs b/src/args.rs index d078ef2..04c7a76 100644 --- a/src/args.rs +++ b/src/args.rs @@ -217,6 +217,12 @@ pub struct CliArgs { )] pub web_upload_concurrency: usize, + /// Enable recursive directory size calculation + /// + /// This is disabled by default because it is a potentially fairly IO intensive operation. + #[arg(long = "directory-size", env = "MINISERVE_DIRECTORY_SIZE")] + pub directory_size: bool, + /// Enable creating directories #[arg( short = 'U', diff --git a/src/config.rs b/src/config.rs index eecc45e..dee69e7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,6 +104,9 @@ pub struct MiniserveConfig { /// Enable QR code display pub show_qrcode: bool, + /// Enable recursive directory size calculation + pub directory_size: bool, + /// Enable creating directories pub mkdir_enabled: bool, @@ -320,6 +323,7 @@ impl MiniserveConfig { pretty_urls: args.pretty_urls, overwrite_files: args.overwrite_files, show_qrcode: args.qrcode, + directory_size: args.directory_size, mkdir_enabled: args.mkdir_enabled, file_upload: args.allowed_upload_dir.is_some(), web_upload_concurrency: args.web_upload_concurrency, diff --git a/src/main.rs b/src/main.rs index cd54bd8..c4432bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -457,28 +457,32 @@ async fn api( ) -> Result<impl Responder, RuntimeError> { match command.into_inner() { ApiCommand::DirSize(path) => { - // The dir argument might be percent-encoded so let's decode it just in case. - let decoded_path = percent_decode_str(&path) - .decode_utf8() - .map_err(|e| RuntimeError::ParseError(path.clone(), e.to_string()))?; - - // Convert the relative dir to an absolute path on the system. - let sanitized_path = file_utils::sanitize_path(&*decoded_path, true) - .expect("Expected a path to directory"); - - let full_path = config - .path - .canonicalize() - .expect("Couldn't canonicalize path") - .join(sanitized_path); - info!("Requested directory listing for {full_path:?}"); - - let dir_size = recursive_dir_size(&full_path).await?; - if config.show_exact_bytes { - Ok(format!("{dir_size} B")) + if config.directory_size { + // The dir argument might be percent-encoded so let's decode it just in case. + let decoded_path = percent_decode_str(&path) + .decode_utf8() + .map_err(|e| RuntimeError::ParseError(path.clone(), e.to_string()))?; + + // Convert the relative dir to an absolute path on the system. + let sanitized_path = file_utils::sanitize_path(&*decoded_path, true) + .expect("Expected a path to directory"); + + let full_path = config + .path + .canonicalize() + .expect("Couldn't canonicalize path") + .join(sanitized_path); + info!("Requested directory listing for {full_path:?}"); + + let dir_size = recursive_dir_size(&full_path).await?; + if config.show_exact_bytes { + Ok(format!("{dir_size} B")) + } else { + let dir_size = ByteSize::b(dir_size); + Ok(dir_size.to_string()) + } } else { - let dir_size = ByteSize::b(dir_size); - Ok(dir_size.to_string()) + Ok("-".to_string()) } } } |