Skip to content

Commit

Permalink
list directory if index not found
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemjay committed Aug 29, 2021
1 parent aade367 commit 1a79dca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let uses_random_route = conf.random_route.clone().is_some();
let full_route = format!("/{}", random_route);

let upload_route;
let upload_route = if let Some(random_route) = conf.random_route.clone() {
format!("/{}/upload", random_route)
} else {
"/upload".to_string()
};

let serve_path = {
let path = &conf.path;
let no_symlinks = conf.no_symlinks;
Expand All @@ -299,29 +304,22 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let dirs_first = conf.dirs_first;
let hide_version_footer = conf.hide_version_footer;
let title = conf.title.clone();
upload_route = if let Some(random_route) = conf.random_route.clone() {
format!("/{}/upload", random_route)
} else {
"/upload".to_string()
};

if path.is_file() {
None
} else if let Some(index_file) = &conf.index {
Some(
actix_files::Files::new(&full_route, path).index_file(index_file.to_string_lossy()),
)
} else {
let u_r = upload_route.clone();
let files;
if show_hidden {
files = actix_files::Files::new(&full_route, path)
.show_files_listing()
.use_hidden_files();
} else {
files = actix_files::Files::new(&full_route, path).show_files_listing();
}

let files = actix_files::Files::new(&full_route, path);
let files = match &conf.index {
Some(index_file) => files.index_file(index_file.to_string_lossy()),
None => files
};
let files = match show_hidden {
true => files.use_hidden_files(),
false => files,
};
let files = files
.show_files_listing()
.files_listing_renderer(move |dir, req| {
listing::directory_listing(
dir,
Expand Down
11 changes: 11 additions & 0 deletions tests/serve_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,14 @@ fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(),

Ok(())
}

#[rstest]
#[case(server(&["--index", FILES[0]]))]
#[case(server(&["--index", "does-not-exist.html"]))]
fn index_fallback_to_listing(#[case] server: TestServer) -> Result<(), Error> {
// If index file is not found, show directory listin instead.
// both cases should return `Ok`
reqwest::blocking::get(server.url())?.error_for_status()?;

Ok(())
}

0 comments on commit 1a79dca

Please sign in to comment.