| 87 | } |
| 88 | |
| 89 | pub async fn cleanup(save_dir: &Path) -> std::io::Result<usize> { |
| 90 | if !save_dir.exists() { |
| 91 | return Ok(0); |
| 92 | } |
| 93 | |
| 94 | let mut entries = fs::read_dir(save_dir).await?; |
| 95 | let mut deleted = 0; |
| 96 | let cutoff = SystemTime::now() |
| 97 | .duration_since(UNIX_EPOCH) |
| 98 | .map(|d| d.as_secs() - CLEANUP_RETENTION_DAYS * 24 * 60 * 60) |
| 99 | .unwrap_or(0); |
| 100 | |
| 101 | while let Some(entry) = entries.next_entry().await? { |
| 102 | let path = entry.path(); |
| 103 | |
| 104 | if path.extension().map(|e| e == "txt").unwrap_or(false) { |
| 105 | if let Ok(metadata) = entry.metadata().await { |
| 106 | if let Ok(modified) = metadata.modified() { |
| 107 | if let Ok(modified_ts) = modified.duration_since(UNIX_EPOCH) { |
| 108 | if modified_ts.as_secs() < cutoff { |
| 109 | fs::remove_file(&path).await?; |
| 110 | deleted += 1; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Ok(deleted) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[cfg(test)] |