peers gone...
This commit is contained in:
@@ -148,6 +148,47 @@ fn cleanup_backup_folder(backup_path: &Path) -> eyre::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn cleanup_failed_download(app_handle: &AppHandle, id: &str) {
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_in_download
|
||||
.write()
|
||||
.await
|
||||
.remove(id);
|
||||
|
||||
let games_folder = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_folder
|
||||
.read()
|
||||
.await
|
||||
.clone();
|
||||
|
||||
if games_folder.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let backup_name = format!("___TO_BE_DELETE___{id}");
|
||||
let backup_path = PathBuf::from(&games_folder).join(&backup_name);
|
||||
let game_path = PathBuf::from(&games_folder).join(id);
|
||||
|
||||
if game_path.exists() {
|
||||
if let Err(e) = std::fs::remove_dir_all(&game_path) {
|
||||
log::error!("Failed to delete half-downloaded game folder: {e}");
|
||||
} else {
|
||||
log::info!(
|
||||
"Deleted half-downloaded game folder: {}",
|
||||
game_path.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = restore_game_folder(&game_path, &backup_path) {
|
||||
log::error!("Failed to restore backup after download failure: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn update_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<bool> {
|
||||
let games_in_download = state.inner().games_in_download.clone();
|
||||
@@ -850,43 +891,23 @@ pub fn run() {
|
||||
log::error!("Failed to emit game-download-failed event: {e}");
|
||||
}
|
||||
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_in_download
|
||||
.write()
|
||||
.await
|
||||
.remove(&id.clone());
|
||||
cleanup_failed_download(&app_handle, &id).await;
|
||||
}
|
||||
PeerEvent::DownloadGameFilesAllPeersGone { id } => {
|
||||
log::warn!(
|
||||
"PeerEvent::DownloadGameFilesAllPeersGone received for {id}"
|
||||
);
|
||||
|
||||
// Check if a backup exists and restore from backup if failed
|
||||
// The backup folder name follows the pattern: ___TO_BE_DELETE___{game_name}
|
||||
let games_folder = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_folder
|
||||
.read()
|
||||
.await
|
||||
.clone();
|
||||
|
||||
if !games_folder.is_empty() {
|
||||
let backup_name = format!("___TO_BE_DELETE___{id}");
|
||||
let backup_path = PathBuf::from(&games_folder).join(backup_name);
|
||||
let game_path = PathBuf::from(&games_folder).join(&id);
|
||||
|
||||
// Delete the half-downloaded game folder if it exists
|
||||
if game_path.exists() {
|
||||
if let Err(e) = std::fs::remove_dir_all(&game_path) {
|
||||
log::error!("Failed to delete half-downloaded game folder: {e}");
|
||||
} else {
|
||||
log::info!("Deleted half-downloaded game folder: {}", game_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
// Restore the backup
|
||||
if let Err(e) = restore_game_folder(&game_path, &backup_path) {
|
||||
log::error!("Failed to restore backup after download failure: {e}");
|
||||
}
|
||||
if let Err(e) = app_handle.emit(
|
||||
"game-download-peers-gone",
|
||||
Some(id.clone()),
|
||||
) {
|
||||
log::error!(
|
||||
"Failed to emit game-download-peers-gone event: {e}"
|
||||
);
|
||||
}
|
||||
|
||||
cleanup_failed_download(&app_handle, &id).await;
|
||||
}
|
||||
PeerEvent::PeerConnected(addr) => {
|
||||
log::info!("Peer connected: {addr}");
|
||||
|
||||
Reference in New Issue
Block a user