From 7f16d08de6e986f533919772801eb24e0af438d7 Mon Sep 17 00:00:00 2001 From: serxoz Date: Fri, 21 Oct 2022 13:36:53 +0200 Subject: [PATCH] xa descarga un arquivo harcodeado --- Cargo.lock | 117 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/main.rs | 5 +- src/vistas/download.rs | 35 ++++++++++++ src/vistas/mod.rs | 1 + 5 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 src/vistas/download.rs diff --git a/Cargo.lock b/Cargo.lock index 2010960..1acdd6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,7 @@ dependencies = [ "bitflags", "bytes", "futures-util", + "headers", "http", "http-body", "hyper", @@ -67,12 +68,27 @@ dependencies = [ "tower-service", ] +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +dependencies = [ + "generic-array", +] + [[package]] name = "bytes" version = "1.2.1" @@ -85,6 +101,35 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "encoding_rs" version = "0.8.31" @@ -124,6 +169,12 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac" +[[package]] +name = "futures-sink" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9" + [[package]] name = "futures-task" version = "0.3.25" @@ -142,6 +193,16 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.8" @@ -153,6 +214,31 @@ dependencies = [ "wasi", ] +[[package]] +name = "headers" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +dependencies = [ + "base64", + "bitflags", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -554,6 +640,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.4" @@ -572,6 +669,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "tokio-util", "tracing", "tracing-subscriber", ] @@ -664,6 +762,19 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tower" version = "0.4.13" @@ -780,6 +891,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + [[package]] name = "unicode-ident" version = "1.0.5" diff --git a/Cargo.toml b/Cargo.toml index 9473c13..073d571 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -axum = { version = "0.5.17", features = ["multipart"]} +axum = { version = "0.5.17", features = ["headers", "multipart"]} serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.68" tokio = { version = "1.0", features = ["full"] } +tokio-util = { version = "0.7", features = ["io"]} tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } rand = "0.8" diff --git a/src/main.rs b/src/main.rs index fa7e231..0c89510 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use axum::{ }; use std::net::SocketAddr; -use crate::vistas::{root::basic, upload::upload}; +use crate::vistas::{root::basic, upload::upload, download::get_file}; #[tokio::main] async fn main() { @@ -18,7 +18,8 @@ async fn main() { let app = Router::new() // `GET /` goes to `root` .route("/", get(basic)) - .route("/u", post(upload)); + .route("/u", post(upload)) + .route("/f/:hash", get(get_file)); // run our app with hyper // `axum::Server` is a re-export of `hyper::Server` diff --git a/src/vistas/download.rs b/src/vistas/download.rs new file mode 100644 index 0000000..b7003b3 --- /dev/null +++ b/src/vistas/download.rs @@ -0,0 +1,35 @@ +use axum::{ + body::StreamBody, + http::{header, StatusCode}, + // http::StatusCode, + // http::{StatusCode, header::{self, HeaderMap, HeaderName}}, + response::IntoResponse, +}; +// use http::{header, StatusCode}; +use tokio_util::io::ReaderStream; + +pub async fn get_file( + axum::extract::Path(_hash): axum::extract::Path +) -> impl IntoResponse { + // hash.into_response() + + // `File` implements `AsyncRead` + let file = match tokio::fs::File::open("Cargo.toml").await { + Ok(file) => file, + Err(err) => return Err((StatusCode::NOT_FOUND, format!("File not found: {}", err))), + }; + // convert the `AsyncRead` into a `Stream` + let stream = ReaderStream::new(file); + // convert the `Stream` into an `axum::body::HttpBody` + let body = StreamBody::new(stream); + + let headers = [ + (header::CONTENT_TYPE, "text/toml; charset=utf-8"), + ( + header::CONTENT_DISPOSITION, + "attachment; filename=\"Cargo.toml\"", + ), + ]; + + Ok((headers, body)) +} diff --git a/src/vistas/mod.rs b/src/vistas/mod.rs index addc5a5..97c1b9f 100644 --- a/src/vistas/mod.rs +++ b/src/vistas/mod.rs @@ -1,2 +1,3 @@ pub mod root; pub mod upload; +pub mod download;