use std::{fs, io::Write as _, path::Path};
use miette::IntoDiagnostic;
use crate::{http_client::create_client_with_user_agent, ok};
pub async fn try_download_file_overwrite_existing(
source_url: &str,
destination_file: impl AsRef<Path>,
) -> miette::Result<()> {
let destination = destination_file.as_ref();
let client = create_client_with_user_agent(None)?;
let response = client.get(source_url).send().await.into_diagnostic()?;
let response = response.error_for_status().into_diagnostic()?;
let response = response.bytes().await.into_diagnostic()?;
let mut dest_file = fs::File::create(destination).into_diagnostic()?;
dest_file.write_all(&response).into_diagnostic()?;
ok!()
}
#[cfg(test)]
mod tests_download {
use std::time::Duration;
use tokio::time::timeout;
use super::*;
use crate::try_create_temp_dir;
const TIMEOUT: Duration = Duration::from_secs(1);
#[tokio::test]
async fn test_download_file_overwrite_existing() {
let root = try_create_temp_dir().unwrap();
let new_dir = root.join("test_download_file_overwrite_existing");
fs::create_dir_all(&new_dir).unwrap();
let source_url = "https://github.com/cloudflare/cfssl/releases/download/v1.6.5/cfssljson_1.6.5_linux_amd64";
let destination_file = new_dir.join("cfssljson");
match timeout(
TIMEOUT,
try_download_file_overwrite_existing(source_url, &destination_file),
)
.await
{
Ok(Ok(_)) => {
assert!(destination_file.exists());
}
Ok(Err(err)) => {
panic!("Error: {err:?}");
}
Err(_) => {
println!("Timeout");
return;
}
}
let meta_data = destination_file.metadata().unwrap();
let og_file_size = meta_data.len();
match timeout(
TIMEOUT,
try_download_file_overwrite_existing(source_url, &destination_file),
)
.await
{
Ok(Ok(_)) => {
assert!(destination_file.exists());
}
Ok(Err(err)) => {
panic!("Error: {err:?}");
}
Err(_) => {
println!("Timeout");
return;
}
}
let meta_data = destination_file.metadata().unwrap();
let new_file_size = meta_data.len();
assert_eq!(og_file_size, new_file_size);
}
}