auto create usercontent path and throw panic only if it is not a directory

This commit is contained in:
blek 2023-10-01 14:43:04 +10:00
parent c6e1f80b47
commit 355f05320b
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 5 additions and 2 deletions

View File

@ -3,7 +3,7 @@
This file provides the `loadenv` function that will do just that. This file provides the `loadenv` function that will do just that.
*/ */
use std::{env::var, net::SocketAddr, path::Path}; use std::{env::var, net::SocketAddr, path::Path, fs};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Redis { pub struct Redis {
@ -45,7 +45,10 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
let spath: String = get_var("USERCONTENT_DIR")?; let spath: String = get_var("USERCONTENT_DIR")?;
let path = Path::new(&spath); let path = Path::new(&spath);
if ! path.exists() { if ! path.exists() {
return Err(format!("USERCONTENT_DIR is set to \"{}\", which does not exist!", &spath).into()) fs::create_dir_all(path)?;
}
if ! path.is_dir() {
return Err(format!("USERCONTENT_DIR is set to \"{}\", which exists but is not a directory!", &spath).into())
} }
spath spath
} }