|
1 | 1 | use crate::admin_server::request::{ |
2 | | - DomainWithOptVersionOption, DomainWithVersionOption, GetDomainOption, GetDomainPositionOption, |
3 | | - UpdateUploadingStatusOption, |
| 2 | + DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption, |
| 3 | + GetDomainOption, GetDomainPositionOption, UpdateUploadingStatusOption, |
4 | 4 | }; |
5 | 5 | use crate::config::AdminConfig; |
6 | 6 | use crate::domain_storage::DomainStorage; |
@@ -45,7 +45,8 @@ impl AdminServer { |
45 | 45 | self.update_domain_version() |
46 | 46 | .or(self.reload_server()) |
47 | 47 | .or(self.change_upload_status()) |
48 | | - .or(self.upload_file()), |
| 48 | + .or(self.upload_file()) |
| 49 | + .or(self.remove_domain_version()), |
49 | 50 | )), |
50 | 51 | ) |
51 | 52 | } |
@@ -150,12 +151,22 @@ impl AdminServer { |
150 | 151 | .and(warp::query::<DomainWithVersionOption>()) |
151 | 152 | .map(service::get_files_metadata) |
152 | 153 | } |
| 154 | + |
| 155 | + fn remove_domain_version( |
| 156 | + &self, |
| 157 | + ) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone { |
| 158 | + warp::path!("files" / "delete") |
| 159 | + .and(with(self.domain_storage.clone())) |
| 160 | + .and(warp::query::<DeleteDomainVersionOption>()) |
| 161 | + .map(service::remove_domain_version) |
| 162 | + } |
153 | 163 | } |
154 | 164 |
|
155 | 165 | pub mod service { |
156 | 166 | use crate::admin_server::request::{ |
157 | | - DomainWithOptVersionOption, DomainWithVersionOption, GetDomainOption, |
158 | | - GetDomainPositionFormat, GetDomainPositionOption, UpdateUploadingStatusOption, |
| 167 | + DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption, |
| 168 | + GetDomainOption, GetDomainPositionFormat, GetDomainPositionOption, |
| 169 | + UpdateUploadingStatusOption, |
159 | 170 | }; |
160 | 171 | use crate::domain_storage::{DomainStorage, URI_REGEX}; |
161 | 172 | use crate::{AdminConfig, HotReloadManager}; |
@@ -184,7 +195,7 @@ pub mod service { |
184 | 195 | return Ok(StatusCode::NOT_FOUND.into_response()); |
185 | 196 | } |
186 | 197 | } |
187 | | - None => Ok(warp::reply::json(&domain_info).into_response()), |
| 198 | + _ => Ok(warp::reply::json(&domain_info).into_response()), |
188 | 199 | } |
189 | 200 | } |
190 | 201 |
|
@@ -344,6 +355,46 @@ pub mod service { |
344 | 355 | } |
345 | 356 | } |
346 | 357 | } |
| 358 | + |
| 359 | + pub(super) fn remove_domain_version( |
| 360 | + storage: Arc<DomainStorage>, |
| 361 | + query: DeleteDomainVersionOption, |
| 362 | + ) -> Response { |
| 363 | + let domains_info = if let Some(domain) = query.domain { |
| 364 | + storage |
| 365 | + .get_domain_info_by_domain(&domain) |
| 366 | + .map(|v| vec![v]) |
| 367 | + .unwrap_or(vec![]) |
| 368 | + } else { |
| 369 | + storage.get_domain_info() |
| 370 | + }; |
| 371 | + for info in domains_info { |
| 372 | + let delete_versions = if let Some(max_reserve) = query.max_reserve { |
| 373 | + if let Some(mut max_version) = |
| 374 | + info.current_version |
| 375 | + .or(info.versions.iter().max().map(|x| *x)) |
| 376 | + { |
| 377 | + max_version = max_version - max_reserve; |
| 378 | + info.versions |
| 379 | + .into_iter() |
| 380 | + .filter(|v| *v < max_version) |
| 381 | + .collect::<Vec<u32>>() |
| 382 | + } else { |
| 383 | + vec![] |
| 384 | + } |
| 385 | + } else { |
| 386 | + let current_version = info.current_version.unwrap_or(u32::MAX); |
| 387 | + info.versions |
| 388 | + .into_iter() |
| 389 | + .filter(|version| *version != current_version) |
| 390 | + .collect::<Vec<u32>>() |
| 391 | + }; |
| 392 | + for version in delete_versions { |
| 393 | + let _ = storage.remove_domain_version(&info.domain, Some(version)); |
| 394 | + } |
| 395 | + } |
| 396 | + Response::default() |
| 397 | + } |
347 | 398 | } |
348 | 399 |
|
349 | 400 | pub mod request { |
@@ -385,12 +436,19 @@ pub mod request { |
385 | 436 | pub domain: String, |
386 | 437 | pub version: Option<u32>, |
387 | 438 | } |
| 439 | + |
388 | 440 | #[derive(Deserialize, Serialize)] |
389 | 441 | pub struct UpdateUploadingStatusOption { |
390 | 442 | pub domain: String, |
391 | 443 | pub version: u32, |
392 | 444 | pub status: UploadingStatus, |
393 | 445 | } |
| 446 | + |
| 447 | + #[derive(Deserialize, Serialize)] |
| 448 | + pub struct DeleteDomainVersionOption { |
| 449 | + pub domain: Option<String>, |
| 450 | + pub max_reserve: Option<u32>, |
| 451 | + } |
394 | 452 | } |
395 | 453 |
|
396 | 454 | // TODO: the code structure is not friendly with Unit Test, need refactor it. |
|
0 commit comments