From c90987d709bb4fbf4a16ef4e5d1b8f041b35ffdc Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Thu, 16 Jul 2020 20:16:17 -0400 Subject: [PATCH] Check extensions case-invariant Ran clippy and linted some of the logic. --- src/main.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index f387310..5cf5e5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,7 @@ async fn get_image( req: web::Query, ) -> impl Responder { if let Some(path) = is_valid_path(&req.path) { - if let Some(_) = req.size { + if req.size.is_some() { let thumbs = dotenv::var("THUMBNAILS").unwrap(); let thumb_path = Path::new(&thumbs).join(path.file_name().unwrap()); @@ -123,12 +123,10 @@ async fn get_image( } else { HttpResponse::NotFound().finish() } - } else { - if let Ok(file) = NamedFile::open(path) { + } else if let Ok(file) = NamedFile::open(path) { file.into_response(&request).unwrap() - } else { - HttpResponse::NotFound().finish() - } + } else { + HttpResponse::NotFound().finish() } } else { HttpResponse::BadRequest().finish() @@ -186,7 +184,7 @@ async fn create_thumbnails() { .read_dir() .expect("Error reading thumbnail directory") .collect(); - if t.len() > 0 { + if !t.is_empty() { println!("Skipping thumbs"); return; } @@ -200,9 +198,12 @@ async fn create_thumbnails() { .filter_map(|entry| entry.ok()) .filter(|entry| { println!("{:?}", entry.path()); - match entry.path().extension() { - Some(ext) if ext == "jpg" || ext == "jpeg" || ext == "png" => true, - _ => false, + if let Some(ext) = entry.path().extension().and_then(|ext| { + ext.to_str().map(|ext| ext.to_lowercase()) + }) { + ext == "jpg" || ext == "jpeg" || ext == "png" + } else { + false } }) .map(|entry| (image::open(entry.path()), entry.path().to_path_buf()))