Simple Download Utility
The simple_download_utility crate provides utilities for downloading files with progress tracking and SHA1 validation support.
Table of Contents
Structures
DownloadProgress
Progress information for a single file download.
| Field | Type | Description |
|---|---|---|
bytes_to_download |
usize |
Total file size in bytes |
bytes_downloaded |
usize |
Bytes downloaded so far |
bytes_per_second |
usize |
Current download speed |
MultiDownloadProgress
Progress information for multiple concurrent file downloads.
| Field | Type | Description |
|---|---|---|
bytes_to_download |
usize |
Total bytes across all files |
bytes_downloaded |
usize |
Total bytes downloaded so far |
bytes_per_second |
usize |
Current aggregate download speed |
files_downloaded |
usize |
Number of files completed |
files_total |
usize |
Total number of files to download |
file_names_downloaded |
Vec<String> |
Names of completed files |
file_names |
Vec<String> |
Names of all files being downloaded |
FileDownloadArguments
Arguments for downloading a single file in a batch operation.
| Field | Type | Description |
|---|---|---|
url |
String |
URL to download from |
sha1 |
Option<String> |
Optional SHA1 hash for validation |
path |
String |
Destination file path |
sender |
Option<Sender<DownloadProgress>> |
Optional progress channel for this file |
Functions
download_file()
Downloads a single file from a URL to a local path.
pub async
Parameters:
url- URL to download frompath- Destination file path (parent directories are created automatically)sender- Optional channel for receiving download progress updates
Returns: Result<()> - Success or error
download_file_with_client()
Downloads a single file using a provided HTTP client.
pub async
Parameters:
client- HTTP client to use (acceptsreqwest::Client,&reqwest::Client, orArc<reqwest::Client>)url- URL to download frompath- Destination file path (parent directories are created automatically)sender- Optional channel for receiving download progress updates
Returns: Result<()> - Success or error
download_and_validate_file()
Downloads a file and validates its SHA1 hash.
pub async
Parameters:
url- URL to download frompath- Destination file pathhash- Expected SHA1 hashsender- Optional channel for receiving download progress updates
Returns: Result<()> - Success or error if hash doesn't match
download_and_validate_file_with_client()
Downloads a file using a provided HTTP client and validates its SHA1 hash.
pub async
Parameters:
client- HTTP client to use (acceptsreqwest::Client,&reqwest::Client, orArc<reqwest::Client>)url- URL to download frompath- Destination file pathhash- Expected SHA1 hashsender- Optional channel for receiving download progress updates
Returns: Result<()> - Success or error if hash doesn't match
download_multiple_files()
Downloads multiple files concurrently with progress tracking.
pub async
Parameters:
items- List of files to downloadparallel- Maximum number of concurrent downloadssender- Optional channel for aggregate progress updates
Returns: Result<()> - Success or first error encountered
download_multiple_files_with_client()
Downloads multiple files concurrently using a provided HTTP client.
pub async
Parameters:
client- HTTP client to use (will be wrapped inArcinternally for sharing across concurrent downloads)items- List of files to downloadparallel- Maximum number of concurrent downloadssender- Optional channel for aggregate progress updates
Returns: Result<()> - Success or first error encountered
Examples
Download Single File Without Progress
use download_file;
async
Download Single File With Progress
use ;
async
Download With SHA1 Validation
use download_and_validate_file;
async
Download With Custom Client
Use the _with_client variants to share a configured HTTP client across multiple downloads:
use download_file_with_client;
use Arc;
use Duration;
async
Download Multiple Files
use ;
async
Progress Tracking Pattern
The download functions use Tokio's mpsc channels for progress reporting. This allows non-blocking progress updates while downloads continue:
// Create a buffered channel
let = ;
// Start download with sender
let download_task = download_file;
// Handle progress in separate task
let progress_task = spawn;
// Wait for download to complete
download_task.await?;
// Progress task will end when channel closes
The buffer size determines how many progress updates can queue before backpressure occurs. A buffer of 10-16 is typically sufficient.