Browse Source

feat: add option --allow-search (#62)

pull/65/head
sigoden 3 years ago committed by GitHub
parent
commit
4058a2db72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      README.md
  2. 2
      assets/index.css
  3. 2
      assets/index.html
  4. 8
      assets/index.js
  5. 8
      src/args.rs
  6. 7
      src/server.rs
  7. 12
      tests/allow.rs
  8. 6
      tests/http.rs

1
README.md

@ -57,6 +57,7 @@ OPTIONS: @@ -57,6 +57,7 @@ OPTIONS:
-A, --allow-all Allow all operations
--allow-upload Allow upload files/folders
--allow-delete Allow delete files/folders
--allow-search Allow search files/folders
--allow-symlink Allow symlink to files/folders outside root directory
--enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *`
--render-index Serve index.html when requesting a directory, returns 404 if not found index.html

2
assets/index.css

@ -10,7 +10,7 @@ body { @@ -10,7 +10,7 @@ body {
}
.hidden {
display: none;
display: none !important;
}
.head {

2
assets/index.html

@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
<input type="file" id="file" name="file" multiple>
</div>
</div>
<form class="searchbar">
<form class="searchbar hidden">
<div class="icon">
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/></svg>
</div>

8
assets/index.js

@ -339,10 +339,14 @@ function ready() { @@ -339,10 +339,14 @@ function ready() {
$uploadersTable = document.querySelector(".uploaders-table");
$emptyFolder = document.querySelector(".empty-folder");
if (params.q) {
document.getElementById('search').value = params.q;
if (DATA.allow_search) {
document.querySelector(".searchbar").classList.remove("hidden");
if (params.q) {
document.getElementById('search').value = params.q;
}
}
addBreadcrumb(DATA.href, DATA.uri_prefix);
if (Array.isArray(DATA.paths)) {
const len = DATA.paths.length;

8
src/args.rs

@ -81,6 +81,11 @@ fn app() -> Command<'static> { @@ -81,6 +81,11 @@ fn app() -> Command<'static> {
.long("allow-delete")
.help("Allow delete files/folders"),
)
.arg(
Arg::new("allow-search")
.long("allow-search")
.help("Allow search files/folders"),
)
.arg(
Arg::new("allow-symlink")
.long("allow-symlink")
@ -136,6 +141,7 @@ pub struct Args { @@ -136,6 +141,7 @@ pub struct Args {
pub auth: AccessControl,
pub allow_upload: bool,
pub allow_delete: bool,
pub allow_search: bool,
pub allow_symlink: bool,
pub render_index: bool,
pub render_spa: bool,
@ -179,6 +185,7 @@ impl Args { @@ -179,6 +185,7 @@ impl Args {
let auth = AccessControl::new(&auth, &uri_prefix)?;
let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload");
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
let allow_search = matches.is_present("allow-all") || matches.is_present("allow-search");
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
let render_index = matches.is_present("render-index");
let render_try_index = matches.is_present("render-try-index");
@ -204,6 +211,7 @@ impl Args { @@ -204,6 +211,7 @@ impl Args {
enable_cors,
allow_delete,
allow_upload,
allow_search,
allow_symlink,
render_index,
render_try_index,

7
src/server.rs

@ -133,6 +133,7 @@ impl Server { @@ -133,6 +133,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 render_index = self.args.render_index;
let render_spa = self.args.render_spa;
let render_try_index = self.args.render_try_index;
@ -152,8 +153,8 @@ impl Server { @@ -152,8 +153,8 @@ impl Server {
.await?;
} else if query == "zip" {
self.handle_zip_dir(path, head_only, &mut res).await?;
} else if let Some(q) = query.strip_prefix("q=") {
let q = decode_uri(q).unwrap_or_default();
} else if allow_search && query.starts_with("q=") {
let q = decode_uri(&query[2..]).unwrap_or_default();
self.handle_query_dir(path, &q, head_only, &mut res).await?;
} else {
self.handle_ls_dir(path, true, head_only, &mut res).await?;
@ -696,6 +697,7 @@ impl Server { @@ -696,6 +697,7 @@ impl Server {
paths,
allow_upload: self.args.allow_upload,
allow_delete: self.args.allow_delete,
allow_search: self.args.allow_search,
dir_exists: exist,
};
let data = serde_json::to_string(&data).unwrap();
@ -824,6 +826,7 @@ struct IndexData { @@ -824,6 +826,7 @@ struct IndexData {
paths: Vec<PathItem>,
allow_upload: bool,
allow_delete: bool,
allow_search: bool,
dir_exists: bool,
}

12
tests/allow.rs

@ -59,3 +59,15 @@ fn allow_upload_delete_can_override(#[with(&["-A"])] server: TestServer) -> Resu @@ -59,3 +59,15 @@ fn allow_upload_delete_can_override(#[with(&["-A"])] server: TestServer) -> Resu
assert_eq!(resp.status(), 201);
Ok(())
}
#[rstest]
fn allow_search(#[with(&["--allow-search"])] server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}?q={}", server.url(), "test.html"))?;
assert_eq!(resp.status(), 200);
let paths = utils::retrive_index_paths(&resp.text()?);
assert!(!paths.is_empty());
for p in paths {
assert!(p.contains(&"test.html"));
}
Ok(())
}

6
tests/http.rs

@ -63,7 +63,7 @@ fn head_dir_zip(server: TestServer) -> Result<(), Error> { @@ -63,7 +63,7 @@ fn head_dir_zip(server: TestServer) -> Result<(), Error> {
}
#[rstest]
fn get_dir_search(server: TestServer) -> Result<(), Error> {
fn get_dir_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}?q={}", server.url(), "test.html"))?;
assert_eq!(resp.status(), 200);
let paths = utils::retrive_index_paths(&resp.text()?);
@ -75,7 +75,7 @@ fn get_dir_search(server: TestServer) -> Result<(), Error> { @@ -75,7 +75,7 @@ fn get_dir_search(server: TestServer) -> Result<(), Error> {
}
#[rstest]
fn get_dir_search2(server: TestServer) -> Result<(), Error> {
fn get_dir_search2(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}?q={}", server.url(), "😀.data"))?;
assert_eq!(resp.status(), 200);
let paths = utils::retrive_index_paths(&resp.text()?);
@ -87,7 +87,7 @@ fn get_dir_search2(server: TestServer) -> Result<(), Error> { @@ -87,7 +87,7 @@ fn get_dir_search2(server: TestServer) -> Result<(), Error> {
}
#[rstest]
fn head_dir_search(server: TestServer) -> Result<(), Error> {
fn head_dir_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
let resp = fetch!(b"HEAD", format!("{}?q={}", server.url(), "test.html")).send()?;
assert_eq!(resp.status(), 200);
assert_eq!(

Loading…
Cancel
Save