more fixes

This commit is contained in:
Robin Appelman 2022-05-23 19:16:13 +02:00
commit 4241314898

View file

@ -1,14 +1,15 @@
use demostf_client::{ApiClient, ListOrder, ListParams}; use demostf_client::{ApiClient, Demo, ListOrder, ListParams};
use main_error::MainError; use main_error::MainError;
use md5::Context; use md5::Context;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{copy, create_dir_all, remove_file, File}; use std::fs::{copy, create_dir_all, remove_file, write, File};
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use thiserror::Error; use thiserror::Error;
use time::OffsetDateTime; use time::OffsetDateTime;
use tracing::{error, info, info_span}; use tokio::time::timeout;
use tracing::{error, info, info_span, instrument, warn};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
@ -78,20 +79,18 @@ async fn main() -> Result<(), MainError> {
.entered(); .entered();
if !source_path.is_file() { if !source_path.is_file() {
error!("source not found"); warn!("source not found, re-downloading");
return Ok(()); re_download(&client, &target_path, &demo).await?;
} }
if target_path.is_file() { if target_path.is_file() {
error!("target exists"); warn!("target exists");
return Ok(()); } else {
}
let calculated_hash = hash(&source_path)?; let calculated_hash = hash(&source_path)?;
if calculated_hash != demo.hash { if calculated_hash != demo.hash {
error!( error!(
calculated = debug(calculated_hash), calculated = debug(calculated_hash),
stored = debug(demo.hash), stored = debug(demo.hash),
"hash mismatch" "hash mismatch for source"
); );
return Ok(()); return Ok(());
} }
@ -99,6 +98,7 @@ async fn main() -> Result<(), MainError> {
create_dir_all(target_path.parent().unwrap())?; create_dir_all(target_path.parent().unwrap())?;
copy(&source_path, &target_path)?; copy(&source_path, &target_path)?;
}
let calculated_hash = hash(&target_path)?; let calculated_hash = hash(&target_path)?;
@ -162,3 +162,16 @@ fn hash<P: AsRef<Path>>(path: P) -> Result<[u8; 16], Error> {
Ok(hash.compute().0) Ok(hash.compute().0)
} }
#[instrument(skip(demo), fields(id = demo.id, target = display(target.display())))]
async fn re_download(client: &ApiClient, target: &Path, demo: &Demo) -> Result<(), Error> {
let mut data = Vec::with_capacity(demo.duration as usize / 60 * 1024);
timeout(Duration::from_secs(5 * 60), demo.save(&client, &mut data))
.await
.map_err(|_| Error::Timeout)??;
write(target, data)?;
Ok(())
}