diff --git a/src/main.rs b/src/main.rs index 0c89510..d1c4ed9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use axum::{ }; use std::net::SocketAddr; -use crate::vistas::{root::basic, upload::upload, download::get_file}; +use crate::vistas::{download::get_file, root::basic, upload::upload}; #[tokio::main] async fn main() { @@ -16,7 +16,6 @@ async fn main() { // build our application with a route let app = Router::new() - // `GET /` goes to `root` .route("/", get(basic)) .route("/u", post(upload)) .route("/f/:hash", get(get_file)); diff --git a/src/vistas/download.rs b/src/vistas/download.rs index d410a03..0a2d8fe 100644 --- a/src/vistas/download.rs +++ b/src/vistas/download.rs @@ -3,17 +3,15 @@ use axum::{ http::{header, StatusCode}, response::IntoResponse, }; -use tokio_util::io::ReaderStream; use tokio::fs::read_dir; +use tokio_util::io::ReaderStream; const BASE_PATH: &str = "uploads"; -pub async fn get_file( - axum::extract::Path(hash): axum::extract::Path -) -> impl IntoResponse { +pub async fn get_file(axum::extract::Path(hash): axum::extract::Path) -> impl IntoResponse { // hash.into_response() let (fpath, fname) = find_file(hash).await; - + // `File` implements `AsyncRead` let file = match tokio::fs::File::open(fpath).await { Ok(file) => file, @@ -35,6 +33,7 @@ pub async fn get_file( Ok((headers, body)) } +// uploads/O/OfsdaDF/proba.txt async fn find_file(hash: String) -> (String, String) { let mut file_path = String::from(""); let mut file_name = String::from(""); diff --git a/src/vistas/mod.rs b/src/vistas/mod.rs index 97c1b9f..547e452 100644 --- a/src/vistas/mod.rs +++ b/src/vistas/mod.rs @@ -1,3 +1,3 @@ +pub mod download; pub mod root; pub mod upload; -pub mod download; diff --git a/src/vistas/upload.rs b/src/vistas/upload.rs index 03f7cbc..ed18f97 100644 --- a/src/vistas/upload.rs +++ b/src/vistas/upload.rs @@ -1,14 +1,9 @@ +use crate::lib::randstr; use axum::{ extract::Multipart, response::{IntoResponse, Response}, }; -use tokio::{ - fs::create_dir_all, - fs::File, - io::AsyncWriteExt, -}; -use crate::lib::randstr; - +use tokio::{fs::create_dir_all, fs::File, io::AsyncWriteExt}; const BASE_PATH: &str = "uploads"; @@ -24,12 +19,14 @@ pub async fn upload(mut multipart: Multipart) -> Response { println!("Length of `{}` is {} bytes", name, data.len()); // tracing::debug!("Length of `{}` is {} bytes", name, data.len()); - let fillo = randstr(); // directorio que aloxará o arquivo - let pai = &fillo[0..1]; // agrupase por directorios co mesmo comezo (1ºchar) + let fillo = randstr(); // directorio que aloxará o arquivo + let pai = &fillo[0..1]; // agrupase por directorios co mesmo comezo (1ºchar) let path = format!("{}/{}/{}", BASE_PATH, pai, fillo); let dest = format!("{}/{}", path, filename); - create_dir_all(&path).await.expect("error creando directorios"); + create_dir_all(&path) + .await + .expect("error creando directorios"); let mut file = File::create(dest).await.expect("error creando arquivo"); file.write_all(&data).await.expect("error gardando contido");