[backup] on the way

This commit is contained in:
2024-11-15 08:59:53 +01:00
parent 2b64d1e4ba
commit f9cd8471b4
3 changed files with 142 additions and 22 deletions

View File

@ -22,6 +22,8 @@ static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../..
pub enum ClientEvent {
ListGames(Vec<Game>),
GotGameFiles(Vec<GameFileDescription>),
DownloadGameFilesBegin { id: String },
DownloadGameFilesFinished { id: String },
}
#[derive(Debug)]
@ -69,6 +71,7 @@ async fn download_game_files(
game_file_descs: Vec<GameFileDescription>,
games_dir: String,
server_addr: SocketAddr,
tx_notify_ui: UnboundedSender<ClientEvent>,
) -> eyre::Result<()> {
let limits = Limits::default()
.with_max_handshake_duration(Duration::from_secs(3))?
@ -89,6 +92,21 @@ async fn download_game_files(
.filter(|desc| !desc.is_dir)
.collect::<Vec<_>>();
if game_file_descs.is_empty() {
log::error!("game_file_descs empty: no game files to download");
return Ok(());
}
let game_id = game_file_descs
.first()
.expect("game_file_descs empty: 2nd case CANNOT HAPPEN")
.game_id
.clone();
tx_notify_ui.send(ClientEvent::DownloadGameFilesBegin {
id: game_id.clone(),
})?;
for file_desc in game_file_descs {
log::info!("downloading file: {}", file_desc.relative_path);
@ -125,6 +143,8 @@ async fn download_game_files(
}
}
log::info!("all files downloaded for game: {game_id}");
tx_notify_ui.send(ClientEvent::DownloadGameFilesFinished { id: game_id })?;
Ok(())
}
@ -205,9 +225,15 @@ pub async fn run(
let games_dir = { ctx.game_dir.lock().await.clone() };
if let Some(games_dir) = games_dir {
let tx_notify_ui = tx_notify_ui.clone();
tokio::task::spawn(async move {
if let Err(e) =
download_game_files(game_file_descs, games_dir, server_addr).await
if let Err(e) = download_game_files(
game_file_descs,
games_dir,
server_addr,
tx_notify_ui,
)
.await
{
log::error!("failed to download game files: {e}");
}