kget/lib.rs
1//! # KGet - A Powerful Download Library for Rust
2//!
3//! `kget` provides robust downloading capabilities for modern applications:
4//!
5//! - **HTTP/HTTPS downloads** with parallel connections (up to 32x speed)
6//! - **FTP/SFTP support** for legacy and secure file transfers
7//! - **BitTorrent** via magnet links with native client (requires `torrent-native` feature)
8//! - **ISO verification** with automatic SHA-256 integrity checking
9//! - **Auto-optimization** based on file type and network conditions
10//!
11//! ## Quick Start
12//!
13//! ```rust,no_run
14//! use kget::{download, DownloadOptions, ProxyConfig, Optimizer};
15//!
16//! // Simple download
17//! let proxy = ProxyConfig::default();
18//! let optimizer = Optimizer::new();
19//! let options = DownloadOptions::default();
20//! download("https://example.com/file.zip", proxy, optimizer, options, None).unwrap();
21//! ```
22//!
23//! ## Advanced Download with Progress
24//!
25//! ```rust,no_run
26//! use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
27//!
28//! let mut downloader = AdvancedDownloader::new(
29//! "https://example.com/large.iso".to_string(),
30//! "large.iso".to_string(),
31//! false,
32//! ProxyConfig::default(),
33//! Optimizer::new(),
34//! );
35//!
36//! // Set progress callback
37//! downloader.set_progress_callback(|progress| {
38//! println!("Progress: {:.1}%", progress * 100.0);
39//! });
40//!
41//! downloader.download().unwrap();
42//! ```
43//!
44//! ## Torrent Downloads
45//!
46//! ```rust,no_run
47//! use kget::torrent::{download_magnet, TorrentCallbacks};
48//! use kget::{ProxyConfig, Optimizer};
49//! use std::sync::Arc;
50//!
51//! let callbacks = TorrentCallbacks {
52//! status: Some(Arc::new(|msg| println!("Status: {}", msg))),
53//! progress: Some(Arc::new(|p| println!("Progress: {:.1}%", p * 100.0))),
54//! };
55//!
56//! download_magnet(
57//! "magnet:?xt=urn:btih:...",
58//! "./downloads",
59//! false,
60//! ProxyConfig::default(),
61//! Optimizer::new(),
62//! callbacks,
63//! ).unwrap();
64//! ```
65//!
66//! ## Features
67//!
68//! - `gui` - Cross-platform GUI using egui (includes `torrent-native`)
69//! - `torrent-native` - Native BitTorrent client using librqbit
70//! - `torrent-transmission` - Transmission RPC integration
71
72// Core modules
73pub mod advanced_download;
74pub mod app;
75pub mod builder;
76pub mod checksum;
77pub mod config;
78pub mod download;
79pub mod error;
80pub mod events;
81pub mod metalink;
82pub mod optimization;
83pub mod progress;
84pub mod queue;
85pub mod utils;
86
87// Protocol modules
88pub mod ftp;
89pub mod sftp;
90pub mod torrent;
91pub mod webdav;
92pub mod ytdlp;
93
94// Re-exports: Configuration
95pub use config::{Config, ProxyConfig, ProxyType};
96
97// Re-exports: Core download functionality
98pub use advanced_download::{AdvancedDownloader, ResumePolicy};
99pub use download::{download, verify_file_sha256, verify_iso_integrity};
100pub use optimization::Optimizer;
101pub use progress::create_progress_bar;
102
103// Re-exports: Torrent types (when available)
104pub use torrent::{TorrentCallbacks, download_magnet};
105
106// Re-exports: Utilities
107pub use utils::{auto_extract, get_filename_from_url_or_default, is_extractable, print, resolve_output_path};
108
109// Re-exports: Protocol helpers
110pub use webdav::is_webdav_url;
111pub use ytdlp::{is_video_url, ytdlp_available, ytdlp_binary};
112
113// Re-exports: High-level builder API (v1.7.0+)
114pub use builder::{
115 Backoff, BatchBuilder, BatchResult, ComputedChecksums, DownloadBuilder,
116 DownloadResult, RetryConfig,
117};
118pub use checksum::ChecksumAlgorithm;
119pub use error::KgetError;
120pub use events::DownloadEvent;
121
122/// Create a [`DownloadBuilder`] for a single URL — the recommended API entry point.
123///
124/// # Example
125///
126/// ```rust,no_run
127/// use kget::KgetError;
128///
129/// let result = kget::builder("https://example.com/file.zip")
130/// .output("./downloads/")
131/// .connections(4)
132/// .sha256("abc123...")
133/// .download()?;
134/// # Ok::<(), KgetError>(())
135/// ```
136pub fn builder(url: impl Into<String>) -> DownloadBuilder {
137 DownloadBuilder::new(url)
138}
139
140/// Create a [`BatchBuilder`] for downloading multiple URLs concurrently.
141///
142/// # Example
143///
144/// ```rust,no_run
145/// let results = kget::batch(["https://a.com/f1.zip", "https://b.com/f2.iso"])
146/// .concurrency(4)
147/// .output_dir("./downloads/")
148/// .download_all();
149/// ```
150pub fn batch(urls: impl IntoIterator<Item = impl Into<String>>) -> BatchBuilder {
151 BatchBuilder::new(urls)
152}
153
154/// Options for configuring a download operation.
155///
156/// # Example
157///
158/// ```rust
159/// use kget::DownloadOptions;
160///
161/// let options = DownloadOptions {
162/// quiet_mode: true,
163/// output_path: Some("./downloads/file.zip".to_string()),
164/// verify_iso: false,
165/// expected_sha256: None,
166/// };
167/// ```
168#[derive(Debug, Clone)]
169pub struct DownloadOptions {
170 /// Suppress progress output to stdout
171 pub quiet_mode: bool,
172 /// Custom output path (uses URL filename if None)
173 pub output_path: Option<String>,
174 /// Automatically verify SHA-256 for ISO files
175 pub verify_iso: bool,
176 /// Expected SHA-256 hash for automatic integrity comparison
177 pub expected_sha256: Option<String>,
178 /// Extra HTTP headers sent with every request (e.g. `("Referer", "https://…")`)
179 pub extra_headers: Vec<(String, String)>,
180}
181
182impl Default for DownloadOptions {
183 fn default() -> Self {
184 Self {
185 quiet_mode: false,
186 output_path: None,
187 verify_iso: false,
188 expected_sha256: None,
189 extra_headers: Vec::new(),
190 }
191 }
192}
193
194/// Type alias for progress callbacks (0.0 to 1.0)
195pub type ProgressCallback = std::sync::Arc<dyn Fn(f32) + Send + Sync>;
196
197/// Type alias for status message callbacks
198pub type StatusCallback = std::sync::Arc<dyn Fn(String) + Send + Sync>;
199
200/// Prelude module for convenient imports
201pub mod prelude {
202 pub use crate::torrent::{TorrentCallbacks, download_magnet};
203 pub use crate::{
204 AdvancedDownloader, Config, DownloadOptions, Optimizer, ProgressCallback, ProxyConfig,
205 ProxyType, StatusCallback, download, verify_iso_integrity,
206 };
207}