From 48b499c349ad2043cae352838bde97f74d4960b3 Mon Sep 17 00:00:00 2001 From: serxoz Date: Fri, 21 Oct 2022 16:18:57 +0200 Subject: [PATCH] error si non se encontra o arquivo --- src/vistas/download.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/vistas/download.rs b/src/vistas/download.rs index 0a2d8fe..dbb8779 100644 --- a/src/vistas/download.rs +++ b/src/vistas/download.rs @@ -9,8 +9,11 @@ use tokio_util::io::ReaderStream; const BASE_PATH: &str = "uploads"; pub async fn get_file(axum::extract::Path(hash): axum::extract::Path) -> impl IntoResponse { - // hash.into_response() - let (fpath, fname) = find_file(hash).await; + // find file from hash + let (fpath, fname) = match find_file(hash).await { + Ok(file) => file, + Err(err) => return Err((StatusCode::NOT_FOUND, format!("File not found: {}", err))), + }; // `File` implements `AsyncRead` let file = match tokio::fs::File::open(fpath).await { @@ -34,16 +37,16 @@ pub async fn get_file(axum::extract::Path(hash): axum::extract::Path) -> } // uploads/O/OfsdaDF/proba.txt -async fn find_file(hash: String) -> (String, String) { +async fn find_file(hash: String) -> Result<(String, String), tokio::io::Error> { let mut file_path = String::from(""); let mut file_name = String::from(""); let pai = &hash[0..1]; //primeiro char let path = format!("{}/{}/{}", BASE_PATH, pai, hash); - let mut dir = read_dir(path).await.expect("error atopando directorio"); - while let Some(child) = dir.next_entry().await.expect("error listando") { + let mut dir = read_dir(path).await?; + while let Some(child) = dir.next_entry().await? { file_path = child.path().display().to_string(); file_name = child.file_name().into_string().unwrap(); } - (file_path, file_name) + Ok((file_path, file_name)) }