| 718 | } |
| 719 | |
| 720 | async fn clear_gource_videos(job_store: web::Data<JobStore>) { |
| 721 | let path = Path::new("/gource_videos"); |
| 722 | let one_hour_ago = SystemTime::now() - Duration::from_secs(3600); |
| 723 | |
| 724 | let mut jobs_to_remove = Vec::new(); |
| 725 | |
| 726 | if let Err(e) = fs::read_dir(path).and_then(|entries| { |
| 727 | entries |
| 728 | .filter_map(Result::ok) |
| 729 | .filter(|entry| { |
| 730 | entry.path().extension().map_or(false, |ext| ext == "mp4") |
| 731 | && entry.metadata().map_or(false, |m| m.is_file()) |
| 732 | }) |
| 733 | .try_for_each(|entry| { |
| 734 | let file_path = entry.path(); |
| 735 | if entry |
| 736 | .metadata() |
| 737 | .and_then(|m| m.created()) |
| 738 | .map_or(false, |created| created < one_hour_ago) |
| 739 | { |
| 740 | fs::remove_file(&file_path).map_err(|e| { |
| 741 | log_message( |
| 742 | log::Level::Error, |
| 743 | &format!("Failed to remove file {:?}: {}", file_path, e), |
| 744 | None, |
| 745 | ); |
| 746 | e |
| 747 | })?; |
| 748 | log_message( |
| 749 | log::Level::Info, |
| 750 | &format!("Removed file: {:?}", file_path), |
| 751 | None, |
| 752 | ); |
| 753 | |
| 754 | if let Some(file_name) = file_path.file_name() { |
| 755 | if let Some(file_name_str) = file_name.to_str() { |
| 756 | if let Some(job_id) = file_name_str |
| 757 | .strip_prefix("gource_") |
| 758 | .and_then(|s| s.strip_suffix(".mp4")) |
| 759 | { |
| 760 | jobs_to_remove.push(job_id.to_string()); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | Ok(()) |
| 766 | }) |
| 767 | }) { |
| 768 | log_message( |
| 769 | log::Level::Error, |
| 770 | &format!("Failed to process directory: {}", e), |
| 771 | None, |
| 772 | ); |
| 773 | } |
| 774 | |
| 775 | let mut store = job_store.lock().await; |
| 776 | for job_id in jobs_to_remove { |
| 777 | if store.remove(&job_id).is_some() { |