From ed16068909e4e7bf2370321c4185773c4ac0485e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD=20=D0=93=D0=B5=D0=BE?= =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8?= Date: Sun, 20 Sep 2020 02:36:43 +0200 Subject: [PATCH] recreate the old directory listing using nice tables with links to the .tar archives --- src/directory_listing.rs | 145 ++++++++++----------------------------- 1 file changed, 35 insertions(+), 110 deletions(-) diff --git a/src/directory_listing.rs b/src/directory_listing.rs index 78ff79e..57922e3 100644 --- a/src/directory_listing.rs +++ b/src/directory_listing.rs @@ -23,7 +23,7 @@ pub fn directory_listing( dir: &Directory, req: &HttpRequest, ) -> Result { - let index_of = format!("Index of {}", req.path()); + let index_of = req.path().trim_end_matches('/'); let mut body = String::new(); let base = Path::new(req.path()); @@ -43,16 +43,18 @@ pub fn directory_listing( if metadata.is_dir() { let _ = write!( body, - "
  • {}/
  • ", + "📂 {}/ [.tar]", encode_file_url!(p), encode_file_name!(entry), + encode_file_url!(p), ); } else { let _ = write!( body, - "
  • {}
  • ", + "🗎 {} {}", encode_file_url!(p), encode_file_name!(entry), + metadata.len(), ); } } else { @@ -61,15 +63,37 @@ pub fn directory_listing( } } - let html = format!( - "\ - {}\ -

    {}

    \ - \n", - index_of, index_of, body + let header = format!( + "

    Index of {}/

    \n\ + [.tar of whole directory]", + index_of, if index_of.is_empty() { "_" } else { index_of } ); + + let footer = format!( + r#""#, + env!("CARGO_PKG_HOMEPAGE"), + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION") + ); + + let style = include_str!("style.css"); + + let html = format!( + "\n\ + \n\ + \n\ + Index of {}\n\ + \n\ + \n{}\n\ + \n\ + \n\ + {}\ +
    📁 ../Size
    \n\ + {}\ + \n", + index_of, style, header, body, footer + ); + Ok(ServiceResponse::new( req.clone(), HttpResponse::Ok() @@ -77,102 +101,3 @@ pub fn directory_listing( .body(html), )) } - -// fn handle_directory( -// dir: &fs::Directory, -// req: &HttpRequest, -// ) -> Result { -// let rd = std::fs::read_dir(&dir.path)?; - -// fn optimistic_is_dir(entry: &std::fs::DirEntry) -> bool { -// // consider it non directory if metadata reading fails, better than an unwrap() panic -// entry -// .metadata() -// .map(|m| m.file_type().is_dir()) -// .unwrap_or(false) -// } -// let mut paths: Vec<_> = rd -// .filter_map(|entry| { -// if dir.is_visible(&entry) { -// entry.ok() -// } else { -// None -// } -// }) -// .collect(); -// paths.sort_by_key(|entry| (!optimistic_is_dir(entry), entry.file_name())); - -// let tar_url = req.path().trim_end_matches('/'); // this is already encoded - -// let mut body = String::new(); -// writeln!(body, "

    Index of {}

    ", req.path()).unwrap(); // FIXME: decode from url, escape for html -// writeln!( -// body, -// r#"[.tar of whole directory]"#, -// tar_url -// ) -// .unwrap(); -// writeln!(body, "").unwrap(); -// writeln!( -// body, -// "" -// ) -// .unwrap(); - -// for entry in paths { -// let meta = entry.metadata()?; -// let file_url = -// utf8_percent_encode(&entry.file_name().to_string_lossy(), NON_ALPHANUMERIC).to_string(); -// let file_name = escape_html_entity(&entry.file_name().to_string_lossy()).to_string(); -// let size = meta.len(); - -// write!(body, "").unwrap(); -// if meta.file_type().is_dir() { -// writeln!( -// body, -// r#""#, -// file_url, file_name -// ) -// .unwrap(); -// write!( -// body, -// r#" "#, -// file_url -// ) -// .unwrap(); -// } else { -// writeln!( -// body, -// r#""#, -// file_url, file_name -// ) -// .unwrap(); -// write!(body, " ", size).unwrap(); -// } -// writeln!(body, "").unwrap(); -// } -// writeln!(body, "
    📁 ../Size
    📂 {}/[.tar]🗎 {}{}
    ").unwrap(); -// writeln!( -// body, -// r#""#, -// env!("CARGO_PKG_HOMEPAGE"), -// env!("CARGO_PKG_NAME"), -// env!("CARGO_PKG_VERSION") -// ) -// .unwrap(); - -// let mut html = String::new(); -// writeln!(html, "").unwrap(); -// writeln!(html, "").unwrap(); -// writeln!(html, "Index of {}", req.path()).unwrap(); -// writeln!(html, "", include_str!("style.css")).unwrap(); -// writeln!(html, "").unwrap(); -// writeln!(html, "\n{}", body).unwrap(); -// writeln!(html, "").unwrap(); - -// let resp = HttpResponse::Ok() -// .content_type("text/html; charset=utf-8") -// .body(html); - -// Ok(ServiceResponse::new(req.clone(), resp)) -// }