Skip to content

Commit

Permalink
fix: fix issues with chinese directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 29, 2024
1 parent e833f5b commit 63406de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ tokio = { version = "1", features = ["full"] }
colored = "2" # 用于输出彩色日志
clap = "4.1" # 解析命令行参数
mime_guess = "2.0" # 用于猜测文件的 MIME 类型
tokio-stream = { version = "0.1", features = ["fs"] }
tokio-stream = { version = "0.1", features = ["fs"] }
percent-encoding = "2.1" # URL 编码
15 changes: 9 additions & 6 deletions src/file_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use warp::Reply;
use std::sync::Arc;
use tokio_stream::wrappers::ReadDirStream;
use tokio_stream::StreamExt;
use percent_encoding::percent_decode_str;
use mime_guess::mime;

pub async fn serve_files(
Expand All @@ -14,14 +15,16 @@ pub async fn serve_files(
enable_cors: bool,
) -> Result<warp::reply::Response, Infallible> {
let path_str = path.as_str();

let full_path = Path::new(&**base_dir).join(path_str);
let full_path_clone = full_path.clone(); // 克隆 PathBuf
// 解码路径
let decoded_path_str = percent_decode_str(path_str).decode_utf8_lossy();
// 转换 Cow<str> 为 &str
let decoded_path = decoded_path_str.as_ref();
let full_path = Path::new(&**base_dir).join(decoded_path);
let full_path_clone = full_path.clone();

let response = if full_path.is_dir() {
match fs::read_dir(full_path).await {
Ok(entries) => {

let mut dir_stream = ReadDirStream::new(entries);
let mut entries_vec: Vec<_> = Vec::new();
while let Some(entry) = dir_stream.next().await {
Expand All @@ -30,10 +33,10 @@ pub async fn serve_files(
Err(e) => eprintln!("Error reading entry: {}", e),
}
}

// Sort the entries
entries_vec.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
let relative_path: String = Path::new(path_str).to_str().unwrap_or(&base_dir).to_string();

let relative_path: String = Path::new(decoded_path).to_str().unwrap_or(&base_dir).to_string();

// 检查 relative_path 是否为空
let relative_path = if relative_path.is_empty() {
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use colored::Colorize;
use warp::Filter;
use warp::http::Method;
use std::sync::Arc;
use percent_encoding::percent_decode_str;

mod file_server;
mod cli;
Expand Down Expand Up @@ -37,11 +38,13 @@ async fn main() {
let css_content_arc = css_content_arc.clone();
move |path: warp::path::Tail, method: Method| {
if !no_request_logging {
// 解码路径
let request_url = percent_decode_str(path.as_str()).decode_utf8_lossy();
// 打印请求方法
println!("{}: {} {}",
" HTTP ".on_blue().white().bold(),
method.to_string().green(),
if path.as_str().is_empty() { "/".green() } else { path.as_str().green() }
if path.as_str().is_empty() { "/".green() } else { request_url.to_string().green() }
);
}
file_server::serve_files(path, css_content_arc.clone(), base_dir.clone(), enable_cors.clone())
Expand Down

0 comments on commit 63406de

Please sign in to comment.