save replay json to disk

master
rascul 2 years ago
parent 96a6658f9b
commit 3855f6ba70

@ -13,6 +13,7 @@ pub async fn post(
) -> Result<HttpResponse, Error> {
let mut wotlog = WotLog::from_payload(payload).await?;
wotlog.parse_log()?;
wotlog.save()?;
let body = format!("{}", to_string_pretty(&wotlog.json)?);

@ -1,10 +1,14 @@
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use actix_multipart::Multipart;
use actix_web::Error;
use chrono::Utc;
use futures_util::StreamExt;
use harsh::Harsh;
use serde_json::{Map, Value};
use serde_json::{Map, Value, to_string_pretty};
use crate::{client, Client};
@ -33,6 +37,18 @@ pub struct WotLog {
}
impl WotLog {
fn generate_id() -> String {
let harsh = Harsh::default();
let ts = Utc::now();
harsh.encode(&[ts.timestamp_nanos() as u64])
}
pub fn new_id(&mut self) {
let harsh = Harsh::default();
let ts = Utc::now();
self.id = harsh.encode(&[ts.timestamp_nanos() as u64])
}
pub fn new() -> Self {
Self {
client: Client::None,
@ -103,4 +119,18 @@ impl WotLog {
Ok(())
}
/// save the json to disk
pub fn save(self: &mut Self) -> Result<(), Error> {
let path = PathBuf::from("replays");
if !path.is_dir() {
std::fs::create_dir(&path)?;
}
let mut file = OpenOptions::new().write(true).create_new(true).open(&path.join(&self.id))?;
file.write_all(to_string_pretty(&self.json)?.as_bytes())?;
Ok(())
}
}

Loading…
Cancel
Save