feat: add option --allow-archive (#152)

BREAKING CHANGE: explicitly allow download folder as zip file
This commit is contained in:
sigoden
2022-12-10 11:09:42 +08:00
committed by GitHub
parent 7eef4407fc
commit 8d9705caa4
8 changed files with 60 additions and 11 deletions

View File

@@ -108,6 +108,12 @@ pub fn build_cli() -> Command {
.action(ArgAction::SetTrue)
.help("Allow symlink to files/folders outside root directory"),
)
.arg(
Arg::new("allow-archive")
.long("allow-archive")
.action(ArgAction::SetTrue)
.help("Allow zip archive generation"),
)
.arg(
Arg::new("enable-cors")
.long("enable-cors")
@@ -191,6 +197,7 @@ pub struct Args {
pub allow_delete: bool,
pub allow_search: bool,
pub allow_symlink: bool,
pub allow_archive: bool,
pub render_index: bool,
pub render_spa: bool,
pub render_try_index: bool,
@@ -244,6 +251,7 @@ impl Args {
let allow_delete = matches.get_flag("allow-all") || matches.get_flag("allow-delete");
let allow_search = matches.get_flag("allow-all") || matches.get_flag("allow-search");
let allow_symlink = matches.get_flag("allow-all") || matches.get_flag("allow-symlink");
let allow_archive = matches.get_flag("allow-all") || matches.get_flag("allow-archive");
let render_index = matches.get_flag("render-index");
let render_try_index = matches.get_flag("render-try-index");
let render_spa = matches.get_flag("render-spa");
@@ -286,6 +294,7 @@ impl Args {
allow_upload,
allow_search,
allow_symlink,
allow_archive,
render_index,
render_try_index,
render_spa,

View File

@@ -182,6 +182,7 @@ impl Server {
let allow_upload = self.args.allow_upload;
let allow_delete = self.args.allow_delete;
let allow_search = self.args.allow_search;
let allow_archive = self.args.allow_archive;
let render_index = self.args.render_index;
let render_spa = self.args.render_spa;
let render_try_index = self.args.render_try_index;
@@ -195,7 +196,11 @@ impl Server {
Method::GET | Method::HEAD => {
if is_dir {
if render_try_index {
if query_params.contains_key("zip") {
if allow_archive && query_params.contains_key("zip") {
if !allow_archive {
status_not_found(&mut res);
return Ok(res);
}
self.handle_zip_dir(path, head_only, &mut res).await?;
} else if allow_search && query_params.contains_key("q") {
self.handle_search_dir(path, &query_params, head_only, &mut res)
@@ -214,6 +219,10 @@ impl Server {
self.handle_render_index(path, &query_params, headers, head_only, &mut res)
.await?;
} else if query_params.contains_key("zip") {
if !allow_archive {
status_not_found(&mut res);
return Ok(res);
}
self.handle_zip_dir(path, head_only, &mut res).await?;
} else if allow_search && query_params.contains_key("q") {
self.handle_search_dir(path, &query_params, head_only, &mut res)
@@ -824,6 +833,7 @@ impl Server {
allow_upload: self.args.allow_upload,
allow_delete: self.args.allow_delete,
allow_search: self.args.allow_search,
allow_archive: self.args.allow_archive,
dir_exists: exist,
};
let data = serde_json::to_string(&data).unwrap();
@@ -984,6 +994,7 @@ struct IndexData {
allow_upload: bool,
allow_delete: bool,
allow_search: bool,
allow_archive: bool,
dir_exists: bool,
}