upload funcionando

This commit is contained in:
serxoz 2022-10-21 12:12:53 +02:00
parent 5c2ba7536f
commit 6ec9cd0560
6 changed files with 89 additions and 10 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
/uploads

48
Cargo.lock generated
View File

@ -142,6 +142,17 @@ dependencies = [
"pin-utils", "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]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@ -401,6 +412,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.47" version = "1.0.47"
@ -419,6 +436,36 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.16" version = "0.2.16"
@ -521,6 +568,7 @@ name = "share"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"rand",
"serde", "serde",
"serde_json", "serde_json",
"tokio", "tokio",

View File

@ -12,3 +12,4 @@ serde_json = "1.0.68"
tokio = { version = "1.0", features = ["full"] } tokio = { version = "1.0", features = ["full"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
rand = "0.8"

9
src/lib.rs Normal file
View File

@ -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()
}

View File

@ -1,3 +1,4 @@
pub mod lib;
pub mod vistas; pub mod vistas;
use axum::{ use axum::{
@ -6,10 +7,7 @@ use axum::{
}; };
use std::net::SocketAddr; use std::net::SocketAddr;
use crate::vistas::{ use crate::vistas::{root::basic, upload::upload};
root::basic,
upload::upload,
};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View File

@ -1,9 +1,21 @@
use axum::extract::Multipart; use axum::{
use tokio::fs::File; extract::Multipart,
use tokio::io::AsyncWriteExt; // for write_all() response::{IntoResponse, Response},
};
use tokio::{
fs::create_dir_all,
fs::File,
io::AsyncWriteExt,
};
use crate::lib::randstr;
const BASE_PATH: &str = "uploads";
// upload view // 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() { while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap().to_string(); let name = field.name().unwrap().to_string();
let filename = field.file_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()); println!("Length of `{}` is {} bytes", name, data.len());
// tracing::debug!("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"); file.write_all(&data).await.expect("error gardando contido");
link = format!("http://localhost:3000/f/{}", fillo);
} }
link.into_response()
} }