From 49b8c334a35bbdd7762ceae93445a051084c31f0 Mon Sep 17 00:00:00 2001
From: KunoiSayami <46131041+KunoiSayami@users.noreply.github.com>
Date: Thu, 13 May 2021 22:40:05 +0800
Subject: feat(core): Use timestamp file to determined should copy or not *
feat(test): Test will check post response validity
---
src/datastructures.rs | 61 ++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 51 insertions(+), 10 deletions(-)
(limited to 'src/datastructures.rs')
diff --git a/src/datastructures.rs b/src/datastructures.rs
index 0a2b9f1..4fa72ab 100644
--- a/src/datastructures.rs
+++ b/src/datastructures.rs
@@ -30,6 +30,7 @@ use std::borrow::Cow;
use std::fmt::Formatter;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
use url::form_urlencoded;
const DEFAULT_CONFIG_LOCATION: &str = "/etc/cgitrc";
@@ -135,16 +136,6 @@ impl Config {
self.database.as_str()
}
- /* pub fn get_secret_warning(&self) -> &str {
- if self.secret.is_empty() {
- r#"Warning: You should specify secret in your cgitrc file."#
- } else if self.secret.len() < MINIMUM_SECRET_LENGTH {
- r#"Warning: You should set key length more than MINIMUM_SECRET_LENGTH."#
- } else {
- ""
- }
- }*/
-
pub fn get_copied_database_location(&self) -> PathBuf {
if self.test {
return PathBuf::from(self.database.as_str());
@@ -165,6 +156,56 @@ impl Config {
test: true,
}
}
+
+ async fn read_timestamp_from_file>(path: P) -> Result {
+ let mut file = tokio::fs::File::open(path).await?;
+ let mut buffer = String::new();
+ file.read_to_string(&mut buffer).await?;
+ Ok(buffer.trim().parse()?)
+ }
+
+ pub async fn get_last_commit_timestamp(&self) -> Result {
+ Self::read_timestamp_from_file(format!(
+ "{}/COMMIT",
+ if self.test { "test" } else { CACHE_DIR }
+ ))
+ .await
+ }
+
+ pub async fn get_last_copy_timestamp(&self) -> Result {
+ Self::read_timestamp_from_file(format!(
+ "{}/COPIED",
+ if self.test { "test" } else { CACHE_DIR }
+ ))
+ .await
+ }
+
+ async fn write_current_timestamp_to_file>(path: P) -> Result<()> {
+ let mut file = tokio::fs::OpenOptions::new()
+ .create(true)
+ .write(true)
+ .open(path)
+ .await?;
+ file.write_all(format!("{}", get_current_timestamp()).as_bytes())
+ .await?;
+ Ok(())
+ }
+
+ pub async fn write_database_commit_timestamp(&self) -> Result<()> {
+ Self::write_current_timestamp_to_file(format!(
+ "{}/COMMIT",
+ if self.test { "test" } else { CACHE_DIR }
+ ))
+ .await
+ }
+
+ pub async fn write_last_copy_timestamp(&self) -> Result<()> {
+ Self::write_current_timestamp_to_file(format!(
+ "{}/COPIED",
+ if self.test { "test" } else { CACHE_DIR }
+ ))
+ .await
+ }
}
#[derive(Debug, Clone, Default)]
--
cgit v1.3.1