From 5aa587f9e9d5f699c5a5fcffa8d32d4974d02221 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sat, 17 Jul 2021 22:07:43 +0200 Subject: [PATCH] refactor: minor syntax improvements on static file module --- src/static_files.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/static_files.rs b/src/static_files.rs index 277c3a9..6c9974f 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -43,7 +43,7 @@ impl AsRef for ArcPath { pub async fn handle( method: &Method, headers: &HeaderMap, - path: impl Into, + base_path: impl Into, uri_path: &str, dir_listing: bool, ) -> Result, StatusCode> { @@ -52,7 +52,7 @@ pub async fn handle( return Err(StatusCode::METHOD_NOT_ALLOWED); } - let base = Arc::new(path.into()); + let base = Arc::new(base_path.into()); let (filepath, meta, auto_index) = path_from_tail(base, uri_path).await?; // Directory listing @@ -190,7 +190,7 @@ async fn read_directory_entries( let mut filesize_str = String::from("-"); if meta.is_dir() { - name = format!("{}/", name); + name += "/"; dirs_count += 1; } else if meta.is_file() { filesize_str = meta @@ -201,7 +201,7 @@ async fn read_directory_entries( } else if meta.file_type().is_symlink() { let m = tokio::fs::symlink_metadata(entry.path().canonicalize()?).await?; if m.is_dir() { - name = format!("{}/", name); + name += "/"; dirs_count += 1; } else { filesize_str = meta @@ -214,7 +214,7 @@ async fn read_directory_entries( continue; } - let uri = format!("{}{}", base_path, name); + let uri = [base_path, &name].concat(); let modified = match parse_last_modified(meta.modified()?) { Ok(tm) => tm.to_local().strftime("%F %T")?.to_string(), Err(err) => { @@ -240,10 +240,9 @@ async fn read_directory_entries( } else { "directories" }; - let files_str = if files_count == 1 { "file" } else { "files" }; let summary_str = format!( "
{} {}, {} {}
", - dirs_count, dirs_str, files_count, files_str + dirs_count, dirs_str, files_count, "file(s)" ); let style_str = r#""#; let footer_str = r#""#; @@ -292,7 +291,7 @@ fn file_reply<'a>( let (path, meta, auto_index) = res; let conditionals = get_conditional_headers(headers); TkFile::open(path.clone()).then(move |res| match res { - Ok(f) => Either::Left(file_conditional(f, path, meta, auto_index, conditionals)), + Ok(file) => Either::Left(file_conditional(file, path, meta, auto_index, conditionals)), Err(err) => { let status = match err.kind() { io::ErrorKind::NotFound => { @@ -418,22 +417,22 @@ impl Conditionals { } async fn file_conditional( - f: TkFile, + file: TkFile, path: ArcPath, meta: &Metadata, auto_index: bool, conditionals: Conditionals, ) -> Result, StatusCode> { - if !auto_index { - Ok(response_body(f, meta, path, conditionals)) - } else { - match f.metadata().await { - Ok(meta) => Ok(response_body(f, &meta, path, conditionals)), + if auto_index { + match file.metadata().await { + Ok(meta) => Ok(response_body(file, &meta, path, conditionals)), Err(err) => { tracing::debug!("file metadata error: {}", err); Err(StatusCode::INTERNAL_SERVER_ERROR) } } + } else { + Ok(response_body(file, meta, path, conditionals)) } } -- libgit2 1.7.2