Fix: Bug with file names

This commit is contained in:
Jonas Zeunert
2024-04-24 00:05:48 +02:00
parent 313faf1864
commit 3046ea6d97
2 changed files with 14 additions and 10 deletions

View File

@@ -21,8 +21,16 @@ impl SearchResult {
} }
impl From<&File> for SearchResult { impl From<&File> for SearchResult {
fn from(file: &File) -> Self { fn from(file: &File) -> Self {
let name = file
.filename
.split('.')
.next()
.ok_or(file.filename.clone())
.unwrap()
.to_string();
Self { Self {
name: file.filename.clone(), name,
modified: file.last_modified.clone(), modified: file.last_modified.clone(),
size: file.filesize, size: file.filesize,
} }

View File

@@ -67,7 +67,7 @@ fn search_to_html(results: Vec<SearchResult>) -> String {
.iter() .iter()
.map(|res| res.to_html()) .map(|res| res.to_html())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join(" \n");
let body = format!( let body = format!(
"<div>Yay, found the following {} lists!</div><div><ul>{}</ul></div>", "<div>Yay, found the following {} lists!</div><div><ul>{}</ul></div>",
results.len(), results.len(),
@@ -75,6 +75,7 @@ fn search_to_html(results: Vec<SearchResult>) -> String {
); );
HTML_TEMPLATE.replace("{body}", &body) HTML_TEMPLATE.replace("{body}", &body)
} }
fn redirect(req: &Request) -> Response { fn redirect(req: &Request) -> Response {
info!("Redirecting request: {:?}", req); info!("Redirecting request: {:?}", req);
@@ -96,18 +97,13 @@ fn redirect(req: &Request) -> Response {
}; };
let Some(first) = response.get(0) else { let Some(first) = response.get(0) else {
error!("No search results"); error!("No search results. Should not happen because we checked before!");
return Response::text("Could not find any awesome page to redirect you to :()") return Response::text("Could not find any awesome page to redirect you to :()")
.with_status_code(404); .with_status_code(404);
}; };
let Some(name) = first.name.split('.').next() else { info!("Redirecting to: /{}", first.name);
error!("Error getting name from response!"); Response::redirect_303(format!("/{}", first.name))
return Response::text("No fitting awesome list found :(").with_status_code(404);
};
info!("Redirecting to: /{}", name);
Response::redirect_303(format!("/{}", name))
} }
fn query_elastic_search(query: String) -> Result<Vec<SearchResult>, Box<dyn Error>> { fn query_elastic_search(query: String) -> Result<Vec<SearchResult>, Box<dyn Error>> {