diff --git a/src/main.rs b/src/main.rs index 628869e..37468e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,8 +82,15 @@ async fn chose_picture(data: web::Data, path: web::Path) -> im } else { println!("Chose \"{}\"", tail); + let path = PathBuf::from(tail.clone()); + + // Extract the filename from the path + let filename = path.file_name() + .and_then(|name| name.to_str()) + .unwrap_or(tail.as_str()); // If converting the path to a &str fails, just use the entire path. + // Find the dimensions of the image. - let (width, height) = match imagesize::size(tail.clone()) { + let (width, height) = match imagesize::size(path.clone()) { Ok(dim) => (dim.width, dim.height), Err(err) => { return HttpResponse::InternalServerError().body(format!("Failure to get dimensions of image ({})!", err)); @@ -106,7 +113,7 @@ async fn chose_picture(data: web::Data, path: web::Path) -> im \"Roulette - ", width, height, tail.clone(), tail); // TODO: Give filename instead of path + ", width, height, tail, filename); HttpResponse::Ok().body(body) } @@ -131,11 +138,24 @@ async fn serve_picture(path: web::Path) -> impl Responder { println!("Serving file \"{}\"", tail); // Read byes of image - let mut file = tokio::fs::File::open(&tail).await.unwrap(); // TODO: Don't unwrap + let mut file = match tokio::fs::File::open(&tail).await { + Ok(file) => file, + Err(e) => { + return HttpResponse::InternalServerError().body(format!("Failure to open file ({})", e)); + } + }; let mut contents = vec![]; - file.read_to_end(&mut contents).await.unwrap(); // TODO: Don't unwrap + if let Err(e) = file.read_to_end(&mut contents).await { + return HttpResponse::InternalServerError().body(format!("Failure to read from file ({})", e)); + } - let content_type = get_image_mime(&tail).unwrap(); // TODO: Don't unwrap + // Get content type + let content_type = match get_image_mime(&tail) { + Ok(ct) => ct, + Err(e) => { + return HttpResponse::InternalServerError().body(format!("Failure to infer content type for image ({})", e)); + } + }; HttpResponse::Ok() .content_type(content_type.unwrap_or_default())