descargando... falta header CONTENT_TYPE
This commit is contained in:
parent
7f16d08de6
commit
c3e9a0b1ee
@ -1,20 +1,21 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
body::StreamBody,
|
body::StreamBody,
|
||||||
http::{header, StatusCode},
|
http::{header, StatusCode},
|
||||||
// http::StatusCode,
|
|
||||||
// http::{StatusCode, header::{self, HeaderMap, HeaderName}},
|
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
};
|
};
|
||||||
// use http::{header, StatusCode};
|
|
||||||
use tokio_util::io::ReaderStream;
|
use tokio_util::io::ReaderStream;
|
||||||
|
use tokio::fs::read_dir;
|
||||||
|
|
||||||
|
const BASE_PATH: &str = "uploads";
|
||||||
|
|
||||||
pub async fn get_file(
|
pub async fn get_file(
|
||||||
axum::extract::Path(_hash): axum::extract::Path<String>
|
axum::extract::Path(hash): axum::extract::Path<String>
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
// hash.into_response()
|
// hash.into_response()
|
||||||
|
let (fpath, fname) = find_file(hash).await;
|
||||||
|
|
||||||
// `File` implements `AsyncRead`
|
// `File` implements `AsyncRead`
|
||||||
let file = match tokio::fs::File::open("Cargo.toml").await {
|
let file = match tokio::fs::File::open(fpath).await {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(err) => return Err((StatusCode::NOT_FOUND, format!("File not found: {}", err))),
|
Err(err) => return Err((StatusCode::NOT_FOUND, format!("File not found: {}", err))),
|
||||||
};
|
};
|
||||||
@ -24,12 +25,26 @@ pub async fn get_file(
|
|||||||
let body = StreamBody::new(stream);
|
let body = StreamBody::new(stream);
|
||||||
|
|
||||||
let headers = [
|
let headers = [
|
||||||
(header::CONTENT_TYPE, "text/toml; charset=utf-8"),
|
// (header::CONTENT_TYPE, "text/toml; charset=utf-8"),
|
||||||
(
|
(
|
||||||
header::CONTENT_DISPOSITION,
|
header::CONTENT_DISPOSITION,
|
||||||
"attachment; filename=\"Cargo.toml\"",
|
format!("attachment; filename=\"{}\"", fname),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
Ok((headers, body))
|
Ok((headers, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn find_file(hash: String) -> (String, String) {
|
||||||
|
let mut file_path = String::from("");
|
||||||
|
let mut file_name = String::from("");
|
||||||
|
let pai = &hash[0..1]; //primeiro char
|
||||||
|
let path = format!("{}/{}/{}", BASE_PATH, pai, hash);
|
||||||
|
|
||||||
|
let mut dir = read_dir(path).await.expect("error atopando directorio");
|
||||||
|
while let Some(child) = dir.next_entry().await.expect("error listando") {
|
||||||
|
file_path = child.path().display().to_string();
|
||||||
|
file_name = child.file_name().into_string().unwrap();
|
||||||
|
}
|
||||||
|
(file_path, file_name)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user