use api-client::save

This commit is contained in:
Robin Appelman 2022-05-09 23:04:44 +02:00
commit 08319e0dc9
5 changed files with 12 additions and 46 deletions

View file

@ -19,19 +19,11 @@ impl Backup {
#[instrument(skip_all, fields(demo.id = demo.id, demo.name = name))]
async fn backup_demo(&self, name: &str, demo: &Demo) -> Result<(), Error> {
info!("backing up");
let chunks = demo.download(&self.client).await?;
let digest = self.store.store(name, chunks).await?;
let mut file = self.store.create(name).await?;
if digest == demo.hash || digest == [0; 16] {
Ok(())
} else {
let _ = self.store.remove(name);
Err(Error::DigestMismatch {
expected: demo.hash,
got: digest,
})
}
demo.save(&self.client, &mut file).await?;
Ok(())
}
#[instrument(skip(self))]

View file

@ -16,8 +16,6 @@ pub enum Error {
Request(#[from] std::io::Error),
#[error(transparent)]
Api(#[from] demostf_client::Error),
#[error("MD5 digest mismatch for downloaded demo, expected {expected:?}, received {got:?}")]
DigestMismatch { expected: [u8; 16], got: [u8; 16] },
}
#[tokio::main]

View file

@ -1,13 +1,8 @@
use crate::Error;
use bytes::Bytes;
use futures_util::{Stream, StreamExt};
use md5::Context;
use std::fs;
use std::fs::{File, Permissions};
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use tokio::pin;
pub struct Store {
basedir: PathBuf,
@ -20,28 +15,14 @@ impl Store {
}
}
pub async fn store(
&self,
name: &str,
data: impl Stream<Item = Result<Bytes, demostf_client::Error>>,
) -> Result<[u8; 16], Error> {
pub async fn create(&self, name: &str) -> Result<File, Error> {
let path = self.generate_path(name);
fs::create_dir_all(path.parent().unwrap())?;
let mut file = File::create(&path)?;
let mut context = Context::new();
pin!(data);
// copy the file and compute the digest was we go
while let Some(chunk) = data.next().await {
let chunk = chunk?;
context.consume(&chunk);
file.write_all(&chunk)?;
}
let file = File::create(&path)?;
file.set_permissions(Permissions::from_mode(0o644))?;
Ok(context.compute().0)
Ok(file)
}
pub fn exists(&self, name: &str) -> bool {