diff --git a/src/routes/submit/submit_post.rs b/src/routes/submit/submit_post.rs index 301fa52..4fc2ccd 100644 --- a/src/routes/submit/submit_post.rs +++ b/src/routes/submit/submit_post.rs @@ -13,6 +13,7 @@ pub async fn post( ) -> Result { let mut wotlog = WotLog::from_payload(payload).await?; wotlog.parse_log()?; + wotlog.save()?; let body = format!("{}", to_string_pretty(&wotlog.json)?); diff --git a/src/wot_log.rs b/src/wot_log.rs index 06514ad..66a30d4 100644 --- a/src/wot_log.rs +++ b/src/wot_log.rs @@ -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(()) + } }