mirror of https://github.com/raftario/filite.git
Merge pull request #13 from steven-joruk/xdg-dirs
Use the correct application directories for the platform
This commit is contained in:
commit
27a9618a55
|
@ -77,6 +77,8 @@ fn main() {
|
||||||
let port = config.port;
|
let port = config.port;
|
||||||
let max_filesize = (config.max_filesize as f64 * 1.37) as usize;
|
let max_filesize = (config.max_filesize as f64 * 1.37) as usize;
|
||||||
|
|
||||||
|
println!("Listening on port {}", port);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(pool.clone())
|
.data(pool.clone())
|
||||||
|
|
71
src/setup.rs
71
src/setup.rs
|
@ -27,25 +27,27 @@ use toml;
|
||||||
/// Returns a path to the directory storing application data
|
/// Returns a path to the directory storing application data
|
||||||
#[cfg(not(feature = "dev"))]
|
#[cfg(not(feature = "dev"))]
|
||||||
pub fn get_data_dir() -> PathBuf {
|
pub fn get_data_dir() -> PathBuf {
|
||||||
let mut dir = dirs::home_dir().expect("Can't find home directory.");
|
let base_dir = dirs::data_dir().expect("Unable to determine the data directory");
|
||||||
dir.push(".filite");
|
base_dir.join(env!("CARGO_PKG_NAME"))
|
||||||
dir
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Returns a path to the configuration file
|
||||||
#[cfg(not(feature = "dev"))]
|
#[cfg(not(feature = "dev"))]
|
||||||
fn get_config_path() -> PathBuf {
|
fn get_config_path() -> PathBuf {
|
||||||
let mut path = get_data_dir();
|
get_config_dir().join("config.toml")
|
||||||
path.push("config.toml");
|
|
||||||
path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a path to the bearer token hash
|
/// Returns a path to the bearer token hash
|
||||||
#[cfg(not(feature = "dev"))]
|
#[cfg(not(feature = "dev"))]
|
||||||
pub fn get_password_path() -> PathBuf {
|
pub fn get_password_path() -> PathBuf {
|
||||||
let mut path = get_data_dir();
|
get_config_dir().join("passwd")
|
||||||
path.push("passwd");
|
|
||||||
path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the BLAKE2b digest of the input string
|
/// Returns the BLAKE2b digest of the input string
|
||||||
|
@ -93,18 +95,13 @@ impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let port = 8080;
|
let port = 8080;
|
||||||
let database_url = {
|
let database_url = {
|
||||||
let mut path = get_data_dir();
|
let path = get_data_dir().join("database.db");
|
||||||
path.push("database.db");
|
|
||||||
path.to_str()
|
path.to_str()
|
||||||
.expect("Can't convert database path to string")
|
.expect("Can't convert database path to string")
|
||||||
.to_owned()
|
.to_owned()
|
||||||
};
|
};
|
||||||
let pool_size = std::cmp::max(1, num_cpus::get() as u32 / 2);
|
let pool_size = std::cmp::max(1, num_cpus::get() as u32 / 2);
|
||||||
let files_dir = {
|
let files_dir = get_data_dir().join("files");
|
||||||
let mut path = get_data_dir();
|
|
||||||
path.push("files");
|
|
||||||
path
|
|
||||||
};
|
|
||||||
let max_filesize = 10_000_000;
|
let max_filesize = 10_000_000;
|
||||||
|
|
||||||
Config {
|
Config {
|
||||||
|
@ -144,14 +141,13 @@ impl Config {
|
||||||
Err(_) => return Err("Invalid files_dir."),
|
Err(_) => return Err("Invalid files_dir."),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut data_dir = get_data_dir();
|
let files_dir = get_data_dir().join(&result.files_dir);
|
||||||
data_dir.push(&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.");
|
return Err("Can't create files_dir.");
|
||||||
}
|
}
|
||||||
|
|
||||||
result.files_dir = match data_dir.canonicalize() {
|
result.files_dir = match files_dir.canonicalize() {
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(_) => return Err("Invalid files_dir."),
|
Err(_) => return Err("Invalid files_dir."),
|
||||||
}
|
}
|
||||||
|
@ -246,19 +242,16 @@ pub fn logger_middleware() -> Logger {
|
||||||
/// Performs the initial setup
|
/// Performs the initial setup
|
||||||
#[cfg(not(feature = "dev"))]
|
#[cfg(not(feature = "dev"))]
|
||||||
pub fn init() -> Config {
|
pub fn init() -> Config {
|
||||||
let data_dir = get_data_dir();
|
fs::create_dir_all(get_config_dir()).unwrap_or_else(|e| {
|
||||||
if !data_dir.exists() {
|
eprintln!("Can't create config directory: {}.", e);
|
||||||
eprintln!("Generating config file...");
|
process::exit(1);
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
let password_path = get_password_path();
|
||||||
|
if !password_path.exists() {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let mut stdin = stdin.lock();
|
let mut stdin = stdin.lock();
|
||||||
eprintln!("Enter the password to use:");
|
println!("Enter the password to use:");
|
||||||
let mut password = String::new();
|
let mut password = String::new();
|
||||||
stdin.read_line(&mut password).unwrap_or_else(|e| {
|
stdin.read_line(&mut password).unwrap_or_else(|e| {
|
||||||
eprintln!("Can't read password: {}", e);
|
eprintln!("Can't read password: {}", e);
|
||||||
|
@ -267,19 +260,21 @@ pub fn init() -> Config {
|
||||||
password = password.replace("\r", "");
|
password = password.replace("\r", "");
|
||||||
password = password.replace("\n", "");
|
password = password.replace("\n", "");
|
||||||
let password_hash = hash(&password);
|
let password_hash = hash(&password);
|
||||||
let password_path = get_password_path();
|
|
||||||
fs::write(&password_path, password_hash.as_slice()).unwrap_or_else(|e| {
|
fs::write(&password_path, password_hash.as_slice()).unwrap_or_else(|e| {
|
||||||
eprintln!("Can't write password: {}", e);
|
eprintln!("Can't write password: {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut config_path = data_dir;
|
let config_path = get_config_path();
|
||||||
config_path.push("config.toml");
|
if !config_path.exists() {
|
||||||
eprintln!(
|
println!("Generating config file at {}", config_path.display());
|
||||||
"Almost ready. To get started, edit the config file at {} and restart.",
|
let config = Config::default();
|
||||||
&config_path.to_str().unwrap(),
|
config.write_file().unwrap_or_else(|e| {
|
||||||
);
|
eprintln!("Can't write config file: {}", e);
|
||||||
process::exit(0);
|
process::exit(1);
|
||||||
|
});
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::read_file().unwrap_or_else(|e| {
|
Config::read_file().unwrap_or_else(|e| {
|
||||||
|
|
Loading…
Reference in New Issue