6 个版本
0.2.4 | 2023年12月30日 |
---|---|
0.2.3 | 2021年3月10日 |
0.2.1 | 2021年2月24日 |
0.1.0 | 2021年2月18日 |
#429 in 文件系统
每月 23 次下载
18KB
396 代码行
Siwi Download
下载文件
Siwi Download 是一个基于 tokio 和 reqwest 构建的下载器。
示例
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use siwi_download::{
download::{Download, DownloadOptions},
error::AnyResult,
};
#[tokio::main]
async fn main() -> AnyResult<()> {
let url = "https://cdn.npm.taobao.org/dist/node/v14.15.4/node-v14.15.4.pkg";
let mut storage_path = std::env::current_dir()?;
storage_path.push("storage");
let storage_path = storage_path.to_str().unwrap();
let mut options = DownloadOptions::default();
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36")?);
options
.set_headers(headers)
.set_file_name("hello_world1.pkg")
.set_show_progress(true);
let download = Download::new(storage_path);
download.auto_create_storage_path().await?;
let report = download.download(url, options)
.await?;
println!("report {:#?}", report);
Ok(())
}
- 编写 CLI 工具
use siwi_download::download::Download;
use siwi_download::download::DownloadOptions;
use siwi_download::error::AnyResult;
#[tokio::main]
async fn main() -> AnyResult<()> {
let args: Vec<String> = std::env::args().collect();
let storage_path = std::env::current_dir()?;
let storage_path = storage_path.to_str().unwrap_or("");
if let Some(url) = args.get(1) {
let mut options = DownloadOptions::default();
options.set_show_progress(true);
let download = Download::new(storage_path);
let report = download.download(url, options).await?;
println!("{:?}", report);
}
Ok(())
}
依赖项
~8–21MB
~306K SLoC