Skip to content

Commit

Permalink
StaticDir filter should filter for list page
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Nov 29, 2023
1 parent 75f064c commit b75a4e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
36 changes: 24 additions & 12 deletions crates/serve-static/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use time::{macros::format_description, OffsetDateTime};

use super::{decode_url_path_safely, encode_url_path, format_url_path_safely, redirect_to_dir_url};
use super::{decode_url_path_safely, encode_url_path, format_url_path_safely, redirect_to_dir_url, join_path};

/// CompressionAlgo
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
Expand Down Expand Up @@ -295,12 +295,6 @@ impl Handler for StaticDir {
decode_url_path_safely(req_path)
};
let rel_path = format_url_path_safely(&rel_path);
for filter in &self.exclude_filters {
if filter(&rel_path) {
res.render(StatusError::not_found());
return;
}
}
let mut files: HashMap<String, Metadata> = HashMap::new();
let mut dirs: HashMap<String, Metadata> = HashMap::new();
let is_dot_file = Path::new(&rel_path)
Expand All @@ -311,7 +305,13 @@ impl Handler for StaticDir {
let mut abs_path = None;
if self.include_dot_files || !is_dot_file {
for root in &self.roots {
let path = root.join(&rel_path);
let raw_path = join_path!(root, &rel_path);
for filter in &self.exclude_filters {
if filter(&raw_path) {
continue;
}
}
let path = Path::new(&raw_path);
if path.is_dir() {
if !req_path.ends_with('/') && !req_path.is_empty() {
redirect_to_dir_url(req.uri(), res);
Expand All @@ -327,22 +327,28 @@ impl Handler for StaticDir {
}

if self.auto_list && abs_path.is_none() {
abs_path = Some(path);
abs_path = Some(path.to_path_buf());
}
if abs_path.is_some() {
break;
}
} else if path.is_file() {
abs_path = Some(path);
abs_path = Some(path.to_path_buf());
}
}
}
let fallback = self.fallback.as_deref().unwrap_or_default();
if abs_path.is_none() && !fallback.is_empty() {
for root in &self.roots {
let path = root.join(fallback);
let raw_path = join_path!(root, fallback);
for filter in &self.exclude_filters {
if filter(&raw_path) {
continue;
}
}
let path = Path::new(&raw_path);
if path.is_file() {
abs_path = Some(path);
abs_path = Some(path.to_path_buf());
break;
}
}
Expand Down Expand Up @@ -423,6 +429,12 @@ impl Handler for StaticDir {
while let Ok(Some(entry)) = entries.next_entry().await {
let file_name = entry.file_name().to_string_lossy().to_string();
if self.include_dot_files || !file_name.starts_with('.') {
let raw_path = join_path!(&abs_path, &file_name);
for filter in &self.exclude_filters {
if filter(&raw_path) {
continue;
}
}
if let Ok(metadata) = entry.metadata().await {
if metadata.is_dir() {
dirs.entry(file_name).or_insert(metadata);
Expand Down
14 changes: 1 addition & 13 deletions crates/serve-static/src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@ use salvo_core::http::header::{CONTENT_TYPE, ETAG, IF_NONE_MATCH};
use salvo_core::http::{Mime, Request, Response, StatusCode};
use salvo_core::{async_trait, Depot, FlowCtrl, Handler, IntoVecString};

use super::{decode_url_path_safely, format_url_path_safely, redirect_to_dir_url};

macro_rules! join_path {
($($part:expr),+) => {
{
let mut p = std::path::PathBuf::new();
$(
p.push($part);
)*
path_slash::PathBufExt::to_slash_lossy(&p).to_string()
}
}
}
use super::{decode_url_path_safely, format_url_path_safely, redirect_to_dir_url, join_path};

/// Handler that serves embed file.
#[non_exhaustive]
Expand Down
14 changes: 14 additions & 0 deletions crates/serve-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ pub use file::StaticFile;
#[macro_use]
mod cfg;

#[doc(hidden)]
#[macro_export]
macro_rules! join_path {
($($part:expr),+) => {
{
let mut p = std::path::PathBuf::new();
$(
p.push($part);
)*
path_slash::PathBufExt::to_slash_lossy(&p).to_string()
}
}
}

cfg_feature! {
#![feature = "embed"]
mod embed;
Expand Down

0 comments on commit b75a4e7

Please sign in to comment.