package download functionality to DaemonService

This commit is contained in:
Noor 2025-01-20 16:50:03 +05:30
parent e76c9cc8cc
commit a709bffb02
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 923 additions and 1 deletions

883
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -12,6 +12,8 @@ tonic = "0.12.3"
# detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" } # detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" }
detee-shared = { path = "../detee-shared" } detee-shared = { path = "../detee-shared" }
chrono = "0.4.39"
reqwest = "0.12.12"
[build-dependencies] [build-dependencies]
tonic-build = "0.12.3" tonic-build = "0.12.3"

@ -1,3 +1,8 @@
use chrono::Utc;
use reqwest::Client;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::{net::SocketAddr, str::FromStr}; use std::{net::SocketAddr, str::FromStr};
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -45,7 +50,39 @@ impl DaemonServicePB for DaemonServer {
request: tonic::Request<Container>, request: tonic::Request<Container>,
) -> Result<tonic::Response<NewContainerRes>, tonic::Status> { ) -> Result<tonic::Response<NewContainerRes>, tonic::Status> {
let req_data = request.into_inner(); let req_data = request.into_inner();
dbg!(req_data); dbg!(&req_data);
if let Some(package_url) = req_data.package_url {
let dir_path = Path::new("./enclave_packages");
fs::create_dir_all(dir_path)?;
let file_name = format!(
"{}-enclave_packager.tar.gz",
Utc::now().format("%Y%m%d%H%M%S")
);
let file_path = dir_path.join(file_name);
match download_file(&package_url, &file_path).await {
Ok(_) => {
println!("Downloaded file to {:?}", file_path);
}
Err(e) => {
println!("Error downloading file: {:?}", e);
return Err(tonic::Status::internal("Error downloading file"));
}
}
}
Ok(tonic::Response::new(NewContainerRes::default())) Ok(tonic::Response::new(NewContainerRes::default()))
} }
} }
async fn download_file(url: &str, file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let response = client.get(url).send().await?;
let data = response.bytes().await?;
let mut file = fs::File::create(file_path)?;
file.write_all(&data)?;
Ok(())
}