mod client;
mod http_file_downloader;
mod options;
mod progress;
pub use client::{HttpClient, HttpResponse};
pub use http_file_downloader::{HttpFileDownloader, ProgressReporter};
pub use options::{DEFAULT_TIMEOUT, DownloadOptions, DownloadResult, MAX_DOWNLOAD_SIZE};
pub use progress::IndicatifProgressReporter;
use crate::error::Result;
use std::time::Duration;
pub fn download_jdk(
package: &crate::models::metadata::JdkMetadata,
no_progress: bool,
timeout_secs: Option<u64>,
) -> Result<DownloadResult> {
let download_url = package.download_url.as_ref().ok_or_else(|| {
crate::error::KopiError::InvalidConfig(
"Missing download URL in package metadata".to_string(),
)
})?;
crate::security::verify_https_security(download_url)?;
let mut downloader = HttpFileDownloader::new();
if let Some(timeout) = timeout_secs {
downloader
.http_client
.set_timeout(Duration::from_secs(timeout));
}
if !no_progress {
downloader = downloader.with_progress_reporter(Box::new(IndicatifProgressReporter::new()));
}
let options = DownloadOptions {
checksum: package.checksum.clone(),
checksum_type: package.checksum_type,
resume: true,
timeout: timeout_secs
.map(Duration::from_secs)
.unwrap_or(DEFAULT_TIMEOUT),
max_size: MAX_DOWNLOAD_SIZE,
};
let temp_dir = tempfile::tempdir()?;
let file_name = download_url.split('/').next_back().unwrap_or("jdk.tar.gz");
let download_path = temp_dir.path().join(file_name);
let result_path = downloader.download(download_url, &download_path, &options)?;
Ok(DownloadResult::new(result_path, temp_dir))
}