Skip to main content

AdvancedDownloader

Struct AdvancedDownloader 

Source
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};
use std::sync::Arc;

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(Arc::new(|p| {
    // p is 0.0 to 1.0
    update_ui_progress(p);
}));

dl.set_status_callback(Arc::new(|msg| {
    log::info!("{}", msg);
}));

dl.download().unwrap();

fn update_ui_progress(p: f32) {
    // Update your UI here
}

Implementations§

Source§

impl AdvancedDownloader

Source

pub fn new( url: String, output_path: String, quiet_mode: bool, proxy_config: ProxyConfig, optimizer: Optimizer, ) -> Self

Create a new AdvancedDownloader instance.

§Arguments
  • url - URL to download from
  • output_path - Local path for the downloaded file
  • quiet_mode - If true, suppress console output
  • proxy_config - Proxy settings (use ProxyConfig::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(),
);
Source

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);
Source

pub fn is_cancelled(&self) -> bool

Check if the download has been cancelled.

Source

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);
});
Source

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| {
    log::info!("Download status: {}", msg);
});
Source

pub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>>

Start the download.

This method:

  1. Checks for existing partial file (resume support)
  2. Queries server for file size and range support
  3. Downloads using parallel connections if supported
  4. 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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more