A website for temporary file- or text hosting
https://trash.ctdo.de/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
use std::env; |
|
|
|
use std::path::PathBuf; |
|
use time::Duration; |
|
use tokio::fs; |
|
|
|
#[derive(Clone)] |
|
pub struct Config { |
|
pub static_dir: PathBuf, |
|
pub files_dir: PathBuf, |
|
pub max_file_size: Option<u64>, |
|
pub no_auth_limits: Option<NoAuthLimits>, |
|
} |
|
|
|
#[derive(Clone)] |
|
pub struct NoAuthLimits { |
|
pub auth_password: String, |
|
pub max_time: Duration, |
|
pub large_file_max_time: Duration, |
|
pub large_file_size: u64, |
|
} |
|
|
|
pub async fn get_config() -> Config { |
|
let max_file_size = env::var("UPLOAD_MAX_BYTES") |
|
.ok() |
|
.and_then(|variable| variable.parse().ok()) |
|
.unwrap_or(8 * 1024 * 1024); |
|
let max_file_size = (max_file_size != 0).then(|| max_file_size); |
|
|
|
let static_dir = |
|
PathBuf::from(env::var("STATIC_DIR").unwrap_or_else(|_| "./static".to_owned())); |
|
let files_dir = PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())); |
|
fs::create_dir_all(&files_dir) |
|
.await |
|
.expect("could not create directory for storing files"); |
|
|
|
let no_auth_limits = get_no_auth_limits(); |
|
|
|
Config { |
|
static_dir, |
|
files_dir, |
|
max_file_size, |
|
no_auth_limits, |
|
} |
|
} |
|
|
|
fn get_no_auth_limits() -> Option<NoAuthLimits> { |
|
match ( |
|
env::var("AUTH_PASSWORD").ok(), |
|
env_number("NO_AUTH_MAX_TIME"), |
|
env_number("NO_AUTH_LARGE_FILE_MAX_TIME"), |
|
env_number("NO_AUTH_LARGE_FILE_SIZE"), |
|
) { |
|
(Some(auth_password), Some(max_time), Some(large_file_max_time), Some(large_file_size)) => { |
|
Some(NoAuthLimits { |
|
auth_password, |
|
max_time: Duration::seconds(max_time as i64), |
|
large_file_max_time: Duration::seconds(large_file_max_time as i64), |
|
large_file_size, |
|
}) |
|
} |
|
(None, None, None, None) => None, |
|
_ => { |
|
panic!("Incomplete NO_AUTH configuration: All environment variables must be specified") |
|
} |
|
} |
|
} |
|
|
|
fn env_number(variable: &str) -> Option<u64> { |
|
env::var(variable).ok().and_then(|n| n.parse::<u64>().ok()) |
|
}
|
|
|