1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::{Download, DownloadSummary, Error, Result};
use crate::progress::Factory;
fn validate_downloads(
downloads: &[Download],
download_folder: &std::path::Path,
factory: &dyn crate::progress::Factory,
) -> Result<Vec<Download>> {
let mut known_urls = std::collections::HashSet::new();
let mut known_download_paths = std::collections::HashSet::new();
let mut result = Vec::with_capacity(downloads.len());
for d in downloads {
if d.urls.is_empty() {
return Err(Error::DownloadDefinition(String::from(
"No URL found to download.",
)));
}
for u in &d.urls {
if !known_urls.insert(u) {
return Err(Error::DownloadDefinition(format!(
"Download URL \"{}\" is used more than once.",
u
)));
}
}
let urls = d.urls.clone();
if d.file_name.to_string_lossy().is_empty() {
return Err(Error::DownloadDefinition(String::from(
"No download file name was provided.",
)));
}
let file_name = download_folder.join(&d.file_name);
if d.file_name.to_string_lossy().is_empty() {
return Err(Error::DownloadDefinition(String::from(
"Failed to get full download path.",
)));
}
if !known_download_paths.insert(&d.file_name) {
return Err(Error::DownloadDefinition(format!(
"Download file name \"{}\" is used more than once.",
d.file_name.to_string_lossy(),
)));
}
let progress = if d.progress.is_none() {
factory.create_reporter()
} else {
d.progress.as_ref().expect("Was Some just now...").clone()
};
result.push(Download {
urls,
file_name,
progress: Some(progress),
verify_callback: d.verify_callback.clone(),
})
}
Ok(result)
}
pub struct Downloader {
client: reqwest::Client,
parallel_requests: u16,
retries: u16,
download_folder: std::path::PathBuf,
}
impl Downloader {
#[must_use]
pub fn builder() -> Builder {
let download_folder =
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(""));
let download_folder = if download_folder.to_string_lossy().is_empty() {
std::path::PathBuf::from(
std::env::var_os("HOME").unwrap_or_else(|| std::ffi::OsString::from("/")),
)
} else {
download_folder
};
Builder {
user_agent: format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
connect_timeout: std::time::Duration::from_secs(30),
timeout: std::time::Duration::from_secs(300),
parallel_requests: 32,
retries: 3,
download_folder,
}
}
pub fn download(&mut self, downloads: &[Download]) -> Result<Vec<Result<DownloadSummary>>> {
#[cfg(feature = "tui")]
let factory = crate::progress::Tui::default();
#[cfg(not(feature = "tui"))]
let factory = crate::progress::Noop::default();
let to_process = validate_downloads(downloads, &self.download_folder, &factory)?;
if to_process.is_empty() {
return Ok(Vec::new());
}
Ok(crate::backend::run(
&mut self.client,
to_process,
self.retries,
self.parallel_requests,
&move || {
factory.join();
},
))
}
}
pub struct Builder {
user_agent: String,
connect_timeout: std::time::Duration,
timeout: std::time::Duration,
parallel_requests: u16,
retries: u16,
download_folder: std::path::PathBuf,
}
impl Builder {
pub fn user_agent(&mut self, user_agent: &str) -> &mut Self {
self.user_agent = user_agent.into();
self
}
pub fn connect_timeout(&mut self, timeout: std::time::Duration) -> &mut Self {
self.connect_timeout = timeout;
self
}
pub fn timeout(&mut self, timeout: std::time::Duration) -> &mut Self {
self.timeout = timeout;
self
}
pub fn parallel_requests(&mut self, count: u16) -> &mut Self {
self.parallel_requests = count;
self
}
pub fn retries(&mut self, count: u16) -> &mut Self {
self.retries = count;
self
}
pub fn download_folder(&mut self, folder: &std::path::Path) -> &mut Self {
self.download_folder = folder.to_path_buf();
self
}
pub fn build(&mut self) -> crate::Result<Downloader> {
let builder = reqwest::Client::builder()
.user_agent(self.user_agent.clone())
.connect_timeout(self.connect_timeout)
.timeout(self.timeout);
let download_folder = &self.download_folder;
if download_folder.to_string_lossy().is_empty() {
return Err(crate::Error::Setup(
"Required \"download_folder\" was not set.".into(),
));
}
if !download_folder.is_dir() {
return Err(Error::Setup(format!(
"Required \"download_folder\" with value \"{}\" is not a folder.",
download_folder.to_string_lossy()
)));
}
Ok(Downloader {
client: builder.build().map_err(|e| {
Error::Setup(format!("Failed to set up backend: {}", e.to_string()))
})?,
parallel_requests: self.parallel_requests,
retries: self.retries,
download_folder: download_folder.to_owned(),
})
}
}