Don't call system time at each iteration of handle_file_cleanup

This commit is contained in:
Sosthene 2024-12-03 12:31:41 +01:00
parent 792d1b788d
commit ccb7c1353e

View File

@ -30,6 +30,7 @@ async fn cleanup_expired_files() {
}
};
let now = system_time_to_unix(SystemTime::now());
while let Some(entry) = entries.next().await {
let e = match entry {
Ok(e) => e,
@ -45,7 +46,7 @@ async fn cleanup_expired_files() {
if let Ok(sub_entry) = sub_entry {
let file_path = sub_entry.path();
if file_path.extension() == Some("meta".as_ref()) {
if let Err(err) = handle_file_cleanup(&file_path).await {
if let Err(err) = handle_file_cleanup(now, &file_path).await {
log::error!("Error cleaning file {:?}: {}", file_path, err);
}
}
@ -229,11 +230,10 @@ async fn handle_retrieve(req: Request<()>) -> tide::Result<Response> {
}
/// Checks a metadata file and deletes the associated data file if expired
async fn handle_file_cleanup(meta_path: &Path) -> Result<(), String> {
async fn handle_file_cleanup(now: u64, meta_path: &Path) -> Result<(), String> {
let meta_content = read_to_string(meta_path).await.map_err(|e| format!("Failed to read metadata: {}", e.to_string()))?;
let metadata: Metadata = serde_json::from_str(&meta_content).map_err(|e| format!("Failed to parse metadata: {}", e.to_string()))?;
let now = system_time_to_unix(SystemTime::now());
if metadata.expires_at < now {
let data_file_path = meta_path.with_extension("");
remove_file(&data_file_path).await.map_err(|e| format!("Failed to remove data file: {}", e.to_string()))?;