diff --git a/config/default.toml b/config/default.toml index 4a39d18..493d211 100644 --- a/config/default.toml +++ b/config/default.toml @@ -34,6 +34,9 @@ password = "password" name = "postgres" pool = 4 +[archive] +base_path = "/tmp/mcaptcha-survey" + [footer] about = "https://mcapthca.org/about" donate = "https://mcapthca.org/donate" diff --git a/src/settings.rs b/src/settings.rs index b147f18..306fe80 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -15,6 +15,7 @@ * along with this program. If not, see . */ use std::env; +use std::fs; use std::path::Path; use config::{Config, ConfigError, Environment, File}; @@ -84,6 +85,25 @@ pub struct Footer { pub thanks: Url, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Archive { + pub base_path: String, +} + +impl Archive { + fn create_archive_base_path(&self) { + let base_path = Path::new(&self.base_path); + if base_path.exists() { + if !base_path.is_dir() { + fs::remove_file(&base_path).unwrap(); + fs::create_dir_all(&base_path).unwrap(); + } + } else { + fs::create_dir_all(&base_path).unwrap(); + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Settings { pub debug: bool, @@ -94,6 +114,7 @@ pub struct Settings { pub support_email: String, pub default_campaign: String, pub footer: Footer, + pub archive: Archive, } #[cfg(not(tarpaulin_include))] @@ -143,8 +164,11 @@ impl Settings { set_database_url(&mut s); - match s.try_into() { - Ok(val) => Ok(val), + match s.try_into::() { + Ok(val) => { + val.archive.create_archive_base_path(); + Ok(val) + }, Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))), } }