No unwraps
This commit is contained in:
parent
e19b0a7de9
commit
1138bae234
30
src/main.rs
30
src/main.rs
|
@ -82,8 +82,15 @@ async fn chose_picture(data: web::Data<AppState>, path: web::Path<String>) -> im
|
||||||
} else {
|
} else {
|
||||||
println!("Chose \"{}\"", tail);
|
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.
|
// 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),
|
Ok(dim) => (dim.width, dim.height),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return HttpResponse::InternalServerError().body(format!("Failure to get dimensions of image ({})!", err));
|
return HttpResponse::InternalServerError().body(format!("Failure to get dimensions of image ({})!", err));
|
||||||
|
@ -106,7 +113,7 @@ async fn chose_picture(data: web::Data<AppState>, path: web::Path<String>) -> im
|
||||||
<img src=\"/img/{}\" width=\"{0}\" height=\"{1}\" alt=\"Roulette selected: {}\">
|
<img src=\"/img/{}\" width=\"{0}\" height=\"{1}\" alt=\"Roulette selected: {}\">
|
||||||
</a>
|
</a>
|
||||||
</body>
|
</body>
|
||||||
</html>", width, height, tail.clone(), tail); // TODO: Give filename instead of path
|
</html>", width, height, tail, filename);
|
||||||
|
|
||||||
HttpResponse::Ok().body(body)
|
HttpResponse::Ok().body(body)
|
||||||
}
|
}
|
||||||
|
@ -131,11 +138,24 @@ async fn serve_picture(path: web::Path<String>) -> impl Responder {
|
||||||
println!("Serving file \"{}\"", tail);
|
println!("Serving file \"{}\"", tail);
|
||||||
|
|
||||||
// Read byes of image
|
// 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![];
|
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()
|
HttpResponse::Ok()
|
||||||
.content_type(content_type.unwrap_or_default())
|
.content_type(content_type.unwrap_or_default())
|
||||||
|
|
Loading…
Reference in New Issue