From 6ec9cd0560c531f3e859da07cd5abc11f6ddce93 Mon Sep 17 00:00:00 2001 From: serxoz Date: Fri, 21 Oct 2022 12:12:53 +0200 Subject: [PATCH] upload funcionando --- .gitignore | 1 + Cargo.lock | 48 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 9 +++++++++ src/main.rs | 6 ++---- src/vistas/upload.rs | 34 +++++++++++++++++++++++++------ 6 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..dccea96 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/uploads diff --git a/Cargo.lock b/Cargo.lock index c1d4825..2010960 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,17 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -401,6 +412,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "proc-macro2" version = "1.0.47" @@ -419,6 +436,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -521,6 +568,7 @@ name = "share" version = "0.1.0" dependencies = [ "axum", + "rand", "serde", "serde_json", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 7f929f3..9473c13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ serde_json = "1.0.68" tokio = { version = "1.0", features = ["full"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } +rand = "0.8" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..fb533c9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,9 @@ +use rand::{distributions::Alphanumeric, Rng}; // 0.8 + +pub fn randstr() -> String { + rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(7) + .map(char::from) + .collect() +} diff --git a/src/main.rs b/src/main.rs index e3ae0ef..fa7e231 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +pub mod lib; pub mod vistas; use axum::{ @@ -6,10 +7,7 @@ use axum::{ }; use std::net::SocketAddr; -use crate::vistas::{ - root::basic, - upload::upload, -}; +use crate::vistas::{root::basic, upload::upload}; #[tokio::main] async fn main() { diff --git a/src/vistas/upload.rs b/src/vistas/upload.rs index 69bb3cb..03f7cbc 100644 --- a/src/vistas/upload.rs +++ b/src/vistas/upload.rs @@ -1,9 +1,21 @@ -use axum::extract::Multipart; -use tokio::fs::File; -use tokio::io::AsyncWriteExt; // for write_all() +use axum::{ + extract::Multipart, + response::{IntoResponse, Response}, +}; +use tokio::{ + fs::create_dir_all, + fs::File, + io::AsyncWriteExt, +}; +use crate::lib::randstr; + + +const BASE_PATH: &str = "uploads"; // upload view -pub async fn upload(mut multipart: Multipart) { +pub async fn upload(mut multipart: Multipart) -> Response { + let mut link = String::from(""); + while let Some(field) = multipart.next_field().await.unwrap() { let name = field.name().unwrap().to_string(); let filename = field.file_name().unwrap().to_string(); @@ -11,8 +23,18 @@ pub async fn upload(mut multipart: Multipart) { println!("Length of `{}` is {} bytes", name, data.len()); // tracing::debug!("Length of `{}` is {} bytes", name, data.len()); - - let mut file = File::create(filename).await.expect("error creando arquivo"); + + 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"); + let mut file = File::create(dest).await.expect("error creando arquivo"); file.write_all(&data).await.expect("error gardando contido"); + + link = format!("http://localhost:3000/f/{}", fillo); } + + link.into_response() }