downloader_http_rs/
lib.rs1pub mod config;
2pub mod http;
3use anyhow::{anyhow, Error};
4use chksum_md5 as md5;
5pub use config::Config;
6pub use http::HttpDownload;
7use std::fs::File;
8use std::path::Path;
9pub fn get_file_md5(file_path: &str) -> Result<String, Error> {
11 if file_path.is_empty() {
12 return Err(anyhow!("file_path is empty"));
13 }
14
15 let path = Path::new(file_path);
16
17 if !path.exists() {
18 return Err(anyhow!("file does not exist"));
19 }
20 let file = File::open(path)?;
21 match md5::chksum(file) {
22 Ok(digest) => Ok(digest.to_string()),
23 Err(e) => Err(anyhow!("get_file_md5 chksum failed:{}", e)),
24 }
25}
26
27pub fn uzip_file(zipfile: &str, to_dir: &str) -> Result<(), Error> {
29 let path = Path::new(&zipfile);
30 if !path.exists() {
31 return Err(anyhow!("file does not exist"));
32 }
33
34 match path.extension() {
35 Some(ext) => {
36 if !ext.eq_ignore_ascii_case("zip") {
37 return Err(anyhow!("file is not zip"));
38 }
39 }
40 _ => {
41 return Err(anyhow!("file is not extension"));
42 }
43 }
44 let file = File::open(zipfile)?;
45 let mut archive = zip::ZipArchive::new(file)?;
46 let to_path = Path::new(to_dir);
47
48 match archive.extract(to_path) {
49 Ok(()) => {}
50 Err(e) => {
51 return Err(anyhow!("extract failed:{}", e));
52 }
53 }
54
55 Ok(())
56}
57
58#[cfg(test)]
59#[allow(dead_code)]
60mod test {
61 use fast_log::config::Config;
62 use fast_log::plugin::file_split::{DateType, KeepType, Rolling, RollingType};
63 use fast_log::plugin::packer::LogPacker;
64 fn init_log() {
65 let cfg = Config::new()
66 .level(log::LevelFilter::Trace)
67 .file_split(
68 "./app.log",
69 Rolling::new(RollingType::ByDate(DateType::Day)),
70 KeepType::KeepNum(3),
71 LogPacker {},
72 )
73 .console();
74 let _ = fast_log::init(cfg);
75 }
76
77 #[tokio::test]
78 async fn test_auto_download() {
79 init_log();
80
81 let filepath = crate::HttpDownload::new()
82 .set_max_retries(3)
83 .set_num_workers(3)
84 .set_file_md5("4f319a7a8cd6d322c6d938f7b8c2adb9".to_owned())
85 .debug(true)
86 .set_save_dir("./temp/".to_owned())
87 .set_url("you url")
88 .await
89 .unwrap_or_else(|e| {
90 println!("程序崩了:{}", e);
91 panic!("{}", e);
92 })
93 .start()
94 .await
95 .unwrap_or_else(|e| {
96 panic!("{}", e);
97 });
98 println!("download ok-->:{}", filepath);
99 }
100
101 fn test_unzip() {
103 println!("unzip ok");
105 }
106
107 async fn test_chunk_download() {
109 init_log();
110
111 let filepath = crate::HttpDownload::new()
112 .set_max_retries(3)
113 .set_num_workers(1)
114 .set_chunk_size(10485760)
115 .debug(true)
116 .set_file_md5("1a4f97564f6127ecfc12d19ed39d0b21".to_owned())
117 .set_save_dir("./temp/".to_owned())
118 .set_url("you download url")
119 .await
120 .unwrap_or_else(|e| {
121 println!("程序崩了:{}", e.to_string());
122 ::std::process::exit(1);
123 })
124 .chunk_download()
125 .await
126 .unwrap_or_else(|e| {
127 panic!("{}", e);
128 });
129
130 println!("download ok-->:{}", filepath);
131 }
132
133 async fn test_download() {
135 init_log();
136
137 let filepath = crate::HttpDownload::new()
138 .set_file_md5("1a4f97564f6127ecfc12d19ed39d0b21".to_owned())
139 .set_save_dir("./temp/".to_owned())
140 .set_url("you download url")
141 .await
142 .unwrap_or_else(|e| {
143 panic!("{}", e);
144 })
145 .gener_download()
146 .await
147 .unwrap_or_else(|e| {
148 println!("程序崩了:{}", e.to_string());
149 ::std::process::exit(1);
150 });
151 println!("gener_download ok-->:{}", filepath);
152 }
153
154 fn test_file_md5() {
156 let file_path = "./temp/CentOS-7-x86_64-DVD-2009.iso";
157 let md5 = crate::get_file_md5(file_path).unwrap_or_else(|_| panic!("get_file_md5 failed"));
158 println!("md5:{}", md5);
160 }
161}