downloader_rs/
download_configuration.rs1use crate::verify::file_verify::FileVerify;
2
3pub struct DownloadConfiguration {
4 pub url: Option<String>,
5 pub temp_path: Option<String>,
6 pub path: Option<String>,
7 pub chunk_size: u64,
8 pub total_length: u64,
9 pub remote_version: i64,
10 pub retry_times_on_failure: u8,
11 pub receive_bytes_per_second: u64,
12 pub timeout: u64,
13 pub range_download: bool,
14 pub chunk_download: bool,
15 pub download_in_memory: bool,
16 pub file_verify: FileVerify,
17}
18
19pub struct DownloadConfigurationBuilder {
20 config: DownloadConfiguration,
21}
22
23impl DownloadConfigurationBuilder {
24 fn new(config: DownloadConfiguration) -> Self {
25 Self {
26 config
27 }
28 }
29
30 pub fn set_url(mut self, url: &str) -> DownloadConfigurationBuilder {
31 self.config.url = Some(url.to_string());
32 self
33 }
34
35 pub fn set_file_path(mut self, path: &str) -> DownloadConfigurationBuilder {
36 self.config.path = Some(path.to_string());
37 self.config.temp_path = Some(format!("{}.temp", path));
38 self
39 }
40
41 pub fn set_remote_version(mut self, version: i64) -> DownloadConfigurationBuilder {
42 self.config.remote_version = version;
43 self
44 }
45
46 pub fn set_range_download(mut self, range_download: bool) -> DownloadConfigurationBuilder {
47 self.config.range_download = range_download;
48 self
49 }
50
51 pub fn set_chunk_download(mut self, chunk_download: bool) -> DownloadConfigurationBuilder {
52 self.config.chunk_download = chunk_download;
53 self
54 }
55
56 pub fn set_chunk_size(mut self, chunk_size: u64) -> DownloadConfigurationBuilder {
57 self.config.chunk_size = chunk_size;
58 self
59 }
60
61 pub fn set_retry_times_on_failure(mut self, retry_times: u8) -> DownloadConfigurationBuilder {
62 self.config.retry_times_on_failure = retry_times;
63 self
64 }
65
66 pub fn set_timeout(mut self, timeout: u64) -> DownloadConfigurationBuilder {
67 self.config.timeout = timeout;
68 self
69 }
70
71 pub fn set_download_speed_limit(mut self, receive_bytes_per_second: u64) -> DownloadConfigurationBuilder {
72 self.config.receive_bytes_per_second = receive_bytes_per_second;
73 self
74 }
75
76 pub fn set_download_in_memory(mut self, download_in_memory: bool) -> DownloadConfigurationBuilder {
77 self.config.download_in_memory = download_in_memory;
78 self
79 }
80
81 pub fn set_file_verify(mut self, file_verify: FileVerify) -> DownloadConfigurationBuilder {
82 self.config.file_verify = file_verify;
83 self
84 }
85
86 pub fn build(self) -> DownloadConfiguration {
87 self.validate()
88 }
89
90 fn validate(self) -> DownloadConfiguration {
91 if self.config.url == None {
92 panic!("Download address not configured.");
93 }
94
95 if !self.config.download_in_memory {
96 if self.config.path == None {
97 panic!("No download path specified.");
98 }
99 }
100
101 self.config
102 }
103}
104
105impl DownloadConfiguration {
106 pub fn new() -> DownloadConfigurationBuilder {
107 let config = DownloadConfiguration {
108 url: None,
109 path: None,
110 temp_path: None,
111 file_verify: FileVerify::None,
112 range_download: true,
113 chunk_download: false,
114 chunk_size: 1024 * 1024 * 5,
115 total_length: 0,
116 remote_version: 0,
117 retry_times_on_failure: 0,
118 receive_bytes_per_second: 0,
119 download_in_memory: false,
120 timeout: 0,
121 };
122 DownloadConfigurationBuilder::new(config)
123 }
124
125 pub fn get_file_path(&self) -> &str {
126 return self.path.as_ref().unwrap().as_str();
127 }
128
129 pub fn get_file_temp_path(&self) -> &str {
130 return self.temp_path.as_ref().unwrap().as_str();
131 }
132
133 pub fn url(&self) -> &str { return self.url.as_ref().unwrap().as_str(); }
134}