formateo e limpeza

This commit is contained in:
serxoz 2022-10-21 15:17:31 +02:00
parent c3e9a0b1ee
commit 1f90332fc1
4 changed files with 13 additions and 18 deletions

View File

@ -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));

View File

@ -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<String>
) -> impl IntoResponse {
pub async fn get_file(axum::extract::Path(hash): axum::extract::Path<String>) -> 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("");

View File

@ -1,3 +1,3 @@
pub mod download;
pub mod root;
pub mod upload;
pub mod download;

View File

@ -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");