2021-04-04 12:30:31 +00:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
use async_std::{fs, path::PathBuf};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
pub files_dir: PathBuf,
|
|
|
|
pub max_file_size: Option<u64>,
|
2021-04-07 22:03:02 +00:00
|
|
|
pub no_auth_limits: Option<NoAuthLimits>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct NoAuthLimits {
|
|
|
|
pub auth_password: String,
|
|
|
|
pub max_time: u64,
|
|
|
|
pub large_file_max_time: u64,
|
|
|
|
pub large_file_size: u64,
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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");
|
|
|
|
|
2021-04-07 22:03:02 +00:00
|
|
|
let no_auth_limits = 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,
|
|
|
|
large_file_max_time,
|
|
|
|
large_file_size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2021-04-04 12:30:31 +00:00
|
|
|
Config {
|
|
|
|
files_dir,
|
|
|
|
max_file_size,
|
2021-04-07 22:03:02 +00:00
|
|
|
no_auth_limits,
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 22:03:02 +00:00
|
|
|
|
|
|
|
fn env_number(variable: &str) -> Option<u64> {
|
|
|
|
env::var(variable).ok().and_then(|n| n.parse::<u64>().ok())
|
|
|
|
}
|