feat(server):提供静态netfilter文件的下载。

This commit is contained in:
徐涛 2024-04-03 22:31:57 +08:00
parent 797ca95e7f
commit 3dfecaee79
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,47 @@
use std::path::PathBuf;
use axum::{
body::Body,
http::{header, StatusCode},
response::{IntoResponse, Response},
routing, Router,
};
use tokio::fs::File;
use tokio_util::io::ReaderStream;
pub struct DownloadHandler {
routes: Router,
}
impl Into<Router> for DownloadHandler {
fn into(self) -> Router {
self.routes
}
}
impl DownloadHandler {
pub fn init() -> Self {
let routes = Router::new().route("/package", routing::get(download_netfiler));
Self { routes }
}
}
async fn download_netfiler() -> Result<impl IntoResponse, StatusCode> {
let netfilter_file_path = PathBuf::from("./netfilter.zip");
if !netfilter_file_path.exists() {
return Err(StatusCode::NOT_FOUND);
}
let file = File::open(netfilter_file_path).await.unwrap();
let stream = ReaderStream::new(file);
let body = Body::from_stream(stream);
let response = Response::new(body);
Ok((
StatusCode::OK,
[(
header::CONTENT_DISPOSITION,
"attachment; filename=netfilter.zip",
)],
response,
))
}

View File

@ -1,3 +1,4 @@
mod downloads;
mod license; mod license;
mod products; mod products;
@ -8,6 +9,7 @@ pub fn controllers() -> Box<dyn Iterator<Item = Box<Router>>> {
let controllers: Vec<Box<Router>> = vec![ let controllers: Vec<Box<Router>> = vec![
Box::new(products::ProductsController::init().into()), Box::new(products::ProductsController::init().into()),
Box::new(license::LicenseController::init().into()), Box::new(license::LicenseController::init().into()),
Box::new(downloads::DownloadHandler::init().into()),
]; ];
Box::from(controllers.into_iter()) Box::from(controllers.into_iter())