Skip to content

Commit 5553d21

Browse files
committed
feat(fs): 新增预压缩gzip文件参数
1 parent f78b7a9 commit 5553d21

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/mods/fs/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ pub struct Param {
2121
/// 指定目录
2222
#[arg(long, short, default_value_t = String::from("."))]
2323
pub dir: String,
24+
25+
/// 指定预压缩的 gzip 文件后缀名,如:.gz .terrain
26+
#[arg(long, short, default_values_t = vec![String::from(".gz"), String::from(".terrain")])]
27+
pub gzip: Vec<String>,
28+
29+
/// 是否即使命中文件后缀名,也要读取文件判断是否为 gzip 文件
30+
#[arg(long = "gf")]
31+
pub gzip_file: bool,
2432
}
2533

2634
pub async fn main(param: Param) -> tokio::io::Result<()> {

src/mods/fs/serve_dir.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ pub async fn add_gzip_encoding(
1818
assert!(uri.starts_with('/'));
1919

2020
let mut response = next.run(request).await;
21-
if response.status() == StatusCode::OK {
22-
let path = format!("{}{}", param.dir, normalize_path(&uri));
23-
macro_log::d!("path: {}", path);
2421

25-
if is_gzip_file(&path).await {
22+
if response.status() == StatusCode::OK && param.gzip.iter().any(|it| uri.ends_with(it)) {
23+
let mut is_gzip = true;
24+
25+
// 即使命中文件后缀名,也要读取文件判断是否为 gzip 文件
26+
if param.gzip_file {
27+
let path = format!("{}{}", param.dir, normalize_path(&uri));
28+
macro_log::d!("path: {}", path);
29+
is_gzip = is_gzip_file(&path).await;
30+
}
31+
32+
if is_gzip {
2633
response
2734
.headers_mut()
2835
.insert(header::CONTENT_ENCODING, HeaderValue::from_static("gzip"));

0 commit comments

Comments
 (0)