formateo e limpeza
This commit is contained in:
parent
c3e9a0b1ee
commit
1f90332fc1
@ -7,7 +7,7 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use std::net::SocketAddr;
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -16,7 +16,6 @@ async fn main() {
|
|||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
|
||||||
.route("/", get(basic))
|
.route("/", get(basic))
|
||||||
.route("/u", post(upload))
|
.route("/u", post(upload))
|
||||||
.route("/f/:hash", get(get_file));
|
.route("/f/:hash", get(get_file));
|
||||||
|
@ -3,14 +3,12 @@ use axum::{
|
|||||||
http::{header, StatusCode},
|
http::{header, StatusCode},
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
};
|
};
|
||||||
use tokio_util::io::ReaderStream;
|
|
||||||
use tokio::fs::read_dir;
|
use tokio::fs::read_dir;
|
||||||
|
use tokio_util::io::ReaderStream;
|
||||||
|
|
||||||
const BASE_PATH: &str = "uploads";
|
const BASE_PATH: &str = "uploads";
|
||||||
|
|
||||||
pub async fn get_file(
|
pub async fn get_file(axum::extract::Path(hash): axum::extract::Path<String>) -> impl IntoResponse {
|
||||||
axum::extract::Path(hash): axum::extract::Path<String>
|
|
||||||
) -> impl IntoResponse {
|
|
||||||
// hash.into_response()
|
// hash.into_response()
|
||||||
let (fpath, fname) = find_file(hash).await;
|
let (fpath, fname) = find_file(hash).await;
|
||||||
|
|
||||||
@ -35,6 +33,7 @@ pub async fn get_file(
|
|||||||
Ok((headers, body))
|
Ok((headers, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// uploads/O/OfsdaDF/proba.txt
|
||||||
async fn find_file(hash: String) -> (String, String) {
|
async fn find_file(hash: String) -> (String, String) {
|
||||||
let mut file_path = String::from("");
|
let mut file_path = String::from("");
|
||||||
let mut file_name = String::from("");
|
let mut file_name = String::from("");
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
|
pub mod download;
|
||||||
pub mod root;
|
pub mod root;
|
||||||
pub mod upload;
|
pub mod upload;
|
||||||
pub mod download;
|
|
||||||
|
@ -1,14 +1,9 @@
|
|||||||
|
use crate::lib::randstr;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Multipart,
|
extract::Multipart,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
use tokio::{
|
use tokio::{fs::create_dir_all, fs::File, io::AsyncWriteExt};
|
||||||
fs::create_dir_all,
|
|
||||||
fs::File,
|
|
||||||
io::AsyncWriteExt,
|
|
||||||
};
|
|
||||||
use crate::lib::randstr;
|
|
||||||
|
|
||||||
|
|
||||||
const BASE_PATH: &str = "uploads";
|
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());
|
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 fillo = randstr(); // directorio que aloxará o arquivo
|
let fillo = randstr(); // directorio que aloxará o arquivo
|
||||||
let pai = &fillo[0..1]; // agrupase por directorios co mesmo comezo (1ºchar)
|
let pai = &fillo[0..1]; // agrupase por directorios co mesmo comezo (1ºchar)
|
||||||
let path = format!("{}/{}/{}", BASE_PATH, pai, fillo);
|
let path = format!("{}/{}/{}", BASE_PATH, pai, fillo);
|
||||||
let dest = format!("{}/{}", path, filename);
|
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");
|
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");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user