Merge pull request #13 from steven-joruk/xdg-dirs

Use the correct application directories for the platform
This commit is contained in:
Raphaël Thériault 2019-10-27 18:49:28 -04:00 committed by GitHub
commit 27a9618a55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 38 deletions

View File

@ -77,6 +77,8 @@ fn main() {
let port = config.port;
let max_filesize = (config.max_filesize as f64 * 1.37) as usize;
println!("Listening on port {}", port);
HttpServer::new(move || {
App::new()
.data(pool.clone())

View File

@ -27,25 +27,27 @@ use toml;
/// Returns a path to the directory storing application data
#[cfg(not(feature = "dev"))]
pub fn get_data_dir() -> PathBuf {
let mut dir = dirs::home_dir().expect("Can't find home directory.");
dir.push(".filite");
dir
let base_dir = dirs::data_dir().expect("Unable to determine the data directory");
base_dir.join(env!("CARGO_PKG_NAME"))
}
/// Returns a path to the directory storing application config
#[cfg(not(feature = "dev"))]
pub fn get_config_dir() -> PathBuf {
let base_dir = dirs::config_dir().expect("Unable to determine the config directory");
base_dir.join(env!("CARGO_PKG_NAME"))
}
/// Returns a path to the configuration file
#[cfg(not(feature = "dev"))]
fn get_config_path() -> PathBuf {
let mut path = get_data_dir();
path.push("config.toml");
path
get_config_dir().join("config.toml")
}
/// Returns a path to the bearer token hash
#[cfg(not(feature = "dev"))]
pub fn get_password_path() -> PathBuf {
let mut path = get_data_dir();
path.push("passwd");
path
get_config_dir().join("passwd")
}
/// Returns the BLAKE2b digest of the input string
@ -93,18 +95,13 @@ impl Default for Config {
fn default() -> Self {
let port = 8080;
let database_url = {
let mut path = get_data_dir();
path.push("database.db");
let path = get_data_dir().join("database.db");
path.to_str()
.expect("Can't convert database path to string")
.to_owned()
};
let pool_size = std::cmp::max(1, num_cpus::get() as u32 / 2);
let files_dir = {
let mut path = get_data_dir();
path.push("files");
path
};
let files_dir = get_data_dir().join("files");
let max_filesize = 10_000_000;
Config {
@ -144,14 +141,13 @@ impl Config {
Err(_) => return Err("Invalid files_dir."),
}
} else {
let mut data_dir = get_data_dir();
data_dir.push(&result.files_dir);
let files_dir = get_data_dir().join(&result.files_dir);
if fs::create_dir_all(&data_dir).is_err() {
if fs::create_dir_all(&files_dir).is_err() {
return Err("Can't create files_dir.");
}
result.files_dir = match data_dir.canonicalize() {
result.files_dir = match files_dir.canonicalize() {
Ok(path) => path,
Err(_) => return Err("Invalid files_dir."),
}
@ -246,19 +242,16 @@ pub fn logger_middleware() -> Logger {
/// Performs the initial setup
#[cfg(not(feature = "dev"))]
pub fn init() -> Config {
let data_dir = get_data_dir();
if !data_dir.exists() {
eprintln!("Generating config file...");
fs::create_dir_all(&data_dir)
.unwrap_or_else(|e| eprintln!("Can't create config directory: {}.", e));
Config::default().write_file().unwrap_or_else(|e| {
eprintln!("{}", e);
process::exit(1);
});
fs::create_dir_all(get_config_dir()).unwrap_or_else(|e| {
eprintln!("Can't create config directory: {}.", e);
process::exit(1);
});
let password_path = get_password_path();
if !password_path.exists() {
let stdin = io::stdin();
let mut stdin = stdin.lock();
eprintln!("Enter the password to use:");
println!("Enter the password to use:");
let mut password = String::new();
stdin.read_line(&mut password).unwrap_or_else(|e| {
eprintln!("Can't read password: {}", e);
@ -267,19 +260,21 @@ pub fn init() -> Config {
password = password.replace("\r", "");
password = password.replace("\n", "");
let password_hash = hash(&password);
let password_path = get_password_path();
fs::write(&password_path, password_hash.as_slice()).unwrap_or_else(|e| {
eprintln!("Can't write password: {}", e);
process::exit(1);
});
}
let mut config_path = data_dir;
config_path.push("config.toml");
eprintln!(
"Almost ready. To get started, edit the config file at {} and restart.",
&config_path.to_str().unwrap(),
);
process::exit(0);
let config_path = get_config_path();
if !config_path.exists() {
println!("Generating config file at {}", config_path.display());
let config = Config::default();
config.write_file().unwrap_or_else(|e| {
eprintln!("Can't write config file: {}", e);
process::exit(1);
});
return config;
}
Config::read_file().unwrap_or_else(|e| {