pub struct AdvancedDownloader { /* private fields */ }Expand description
High-performance downloader with parallel connections and resume support.
AdvancedDownloader is the recommended way to download large files. It provides:
- Parallel chunk downloads: Splits files into segments downloaded simultaneously
- Automatic resume: Detects existing partial files and resumes from last position
- Server compatibility: Falls back to single-stream if server doesn’t support ranges
- ISO optimization: Disables compression for binary files to prevent corruption
- Progress tracking: Real-time callbacks for UI integration
- Cancellation support: Stop downloads gracefully via atomic cancel token
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
let downloader = AdvancedDownloader::new(
"https://example.com/large-file.zip".to_string(),
"large-file.zip".to_string(),
false, // quiet_mode
ProxyConfig::default(),
Optimizer::new(),
);
downloader.download().expect("Download failed");§With Progress Tracking
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
let mut dl = AdvancedDownloader::new(
"https://example.com/file.iso".to_string(),
"file.iso".to_string(),
true, // quiet mode (no stdout)
ProxyConfig::default(),
Optimizer::new(),
);
dl.set_progress_callback(|p| {
// p is 0.0 to 1.0
update_ui_progress(p);
});
dl.set_status_callback(|msg| println!("{}", msg));
dl.download().unwrap();
fn update_ui_progress(p: f32) {
// Update your UI here
}Implementations§
Source§impl AdvancedDownloader
impl AdvancedDownloader
Sourcepub fn new(
url: String,
output_path: String,
quiet_mode: bool,
proxy_config: ProxyConfig,
optimizer: Optimizer,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn new( url: String, output_path: String, quiet_mode: bool, proxy_config: ProxyConfig, optimizer: Optimizer, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Create a new AdvancedDownloader instance.
§Arguments
url- URL to download fromoutput_path- Local path for the downloaded filequiet_mode- If true, suppress console outputproxy_config- Proxy settings (useProxyConfig::default()for direct connection)optimizer- Optimizer for connection settings
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
let dl = AdvancedDownloader::new(
"https://example.com/file.zip".to_string(),
"./downloads/file.zip".to_string(),
false,
ProxyConfig::default(),
Optimizer::new(),
);Sourcepub fn set_cancel_token(&mut self, token: Arc<AtomicBool>)
pub fn set_cancel_token(&mut self, token: Arc<AtomicBool>)
Set a custom cancellation token for graceful download interruption.
When the token is set to true, the download will stop at the next
checkpoint and return an error.
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
let cancel = Arc::new(AtomicBool::new(false));
let mut dl = AdvancedDownloader::new(/* ... */
);
dl.set_cancel_token(cancel.clone());
// In another thread:
// cancel.store(true, std::sync::atomic::Ordering::Relaxed);Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if the download has been cancelled.
Sourcepub fn set_progress_callback(
&mut self,
callback: impl Fn(f32) + Send + Sync + 'static,
)
pub fn set_progress_callback( &mut self, callback: impl Fn(f32) + Send + Sync + 'static, )
Set a callback for progress updates.
The callback receives a value from 0.0 (start) to 1.0 (complete).
§Example
dl.set_progress_callback(|progress| {
println!("Downloaded: {:.1}%", progress * 100.0);
});Sourcepub fn set_status_callback(
&mut self,
callback: impl Fn(String) + Send + Sync + 'static,
)
pub fn set_status_callback( &mut self, callback: impl Fn(String) + Send + Sync + 'static, )
Set a callback for status messages.
Receives human-readable status updates during the download.
§Example
dl.set_status_callback(|msg| {
println!("Download status: {}", msg);
});Sourcepub fn set_expected_sha256(&mut self, expected_sha256: impl Into<String>)
pub fn set_expected_sha256(&mut self, expected_sha256: impl Into<String>)
Set an expected SHA-256 hash for automatic verification after download.
Sourcepub fn set_extra_headers(&mut self, headers: Vec<(String, String)>)
pub fn set_extra_headers(&mut self, headers: Vec<(String, String)>)
Set extra HTTP headers sent with every request (e.g. Referer or Cookie).
Sourcepub fn set_resume_policy(&mut self, policy: ResumePolicy)
pub fn set_resume_policy(&mut self, policy: ResumePolicy)
Set how the downloader handles interactive prompts.
Library and automation callers must set ResumePolicy::AlwaysResume to
avoid blocking on stdin.
Sourcepub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>>
Start the download.
This method:
- Checks for existing partial file (resume support)
- Queries server for file size and range support
- Downloads using parallel connections if supported
- Falls back to single-stream if server doesn’t support ranges
§Returns
Returns Ok(()) on successful download, or an error if the download fails.
§Errors
- Network connection failures
- Existing file larger than remote (corrupted state)
- Cancellation via cancel token
- Disk I/O errors
Auto Trait Implementations§
impl !RefUnwindSafe for AdvancedDownloader
impl !UnwindSafe for AdvancedDownloader
impl Freeze for AdvancedDownloader
impl Send for AdvancedDownloader
impl Sync for AdvancedDownloader
impl Unpin for AdvancedDownloader
impl UnsafeUnpin for AdvancedDownloader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more