From 62bff477504e733b274148ec086244a00f0a26cd Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Sat, 25 Nov 2023 07:34:20 +0800 Subject: [PATCH] Fix static dir set dot files not working --- crates/serve-static/src/dir.rs | 16 +++++++--------- examples/static-dir-list/src/main.rs | 1 + .../static-dir-list/static/boy/.dot/dotfile.txt | 5 +++++ 3 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 examples/static-dir-list/static/boy/.dot/dotfile.txt diff --git a/crates/serve-static/src/dir.rs b/crates/serve-static/src/dir.rs index b4660d934..878f5e4ba 100644 --- a/crates/serve-static/src/dir.rs +++ b/crates/serve-static/src/dir.rs @@ -268,16 +268,14 @@ impl Handler for StaticDir { // list the dir if let Ok(mut entries) = tokio::fs::read_dir(&abs_path).await { while let Ok(Some(entry)) = entries.next_entry().await { - if let Ok(metadata) = entry.metadata().await { - if metadata.is_dir() { - dirs.entry(entry.file_name().to_string_lossy().to_string()) - .or_insert(metadata); - } else { - let file_name = entry.file_name().to_string_lossy().to_string(); - if !self.dot_files && file_name.starts_with('.') { - continue; + let file_name = entry.file_name().to_string_lossy().to_string(); + if self.dot_files || (!self.dot_files && !file_name.starts_with('.')) { + if let Ok(metadata) = entry.metadata().await { + if metadata.is_dir() { + dirs.entry(file_name).or_insert(metadata); + } else { + files.entry(file_name).or_insert(metadata); } - files.entry(file_name).or_insert(metadata); } } } diff --git a/examples/static-dir-list/src/main.rs b/examples/static-dir-list/src/main.rs index ecfe429e7..80b3f57ca 100644 --- a/examples/static-dir-list/src/main.rs +++ b/examples/static-dir-list/src/main.rs @@ -12,6 +12,7 @@ async fn main() { "static/boy", "static/girl", ]) + .dot_files(true) .defaults("index.html") .listing(true), ); diff --git a/examples/static-dir-list/static/boy/.dot/dotfile.txt b/examples/static-dir-list/static/boy/.dot/dotfile.txt new file mode 100644 index 000000000..9ba8d0ae2 --- /dev/null +++ b/examples/static-dir-list/static/boy/.dot/dotfile.txt @@ -0,0 +1,5 @@ +Dot file + +An elephant said to a mouse ,"no doubt that you are the smallest znd most useless thing that Ihave e ver seen ." + +"Pless ,say it again .Let me take it down ."the mouse said ."I will tell a flea what I know. \ No newline at end of file