upload funcionando
This commit is contained in:
parent
5c2ba7536f
commit
6ec9cd0560
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
/uploads
|
||||
|
48
Cargo.lock
generated
48
Cargo.lock
generated
@ -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",
|
||||
|
@ -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"
|
||||
|
9
src/lib.rs
Normal file
9
src/lib.rs
Normal 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()
|
||||
}
|
@ -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() {
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user