error si non se encontra o arquivo
This commit is contained in:
parent
1f90332fc1
commit
48b499c349
@ -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<String>) -> 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<String>) ->
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user