diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2025-03-20 12:48:26 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2025-03-20 12:48:30 +0000 |
commit | 900c0035b374845a9af505748ad54c97c1e916e8 (patch) | |
tree | 6d189237c998db7c6580f41a35acd13b445c661d /src/main.rs | |
parent | Merge pull request #1488 from AlecDivito/issue-1485 (diff) | |
download | miniserve-900c0035b374845a9af505748ad54c97c1e916e8.tar.gz miniserve-900c0035b374845a9af505748ad54c97c1e916e8.zip |
Add --directory-size flag to enable directory size calculation
This is turned off by default as it's potentially quite IO intensive, especially on HDDs.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 25 insertions, 21 deletions
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()) } } } |