feat(configuration): Add global configuration

also update some error messages
This commit is contained in:
Alexander Navarro 2024-04-29 19:22:45 -04:00
parent 8cd4b4b10f
commit f0485abfbb
6 changed files with 67 additions and 22 deletions

View file

@ -1,8 +1,10 @@
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use std::{io, path::PathBuf};
use std::path::PathBuf;
pub fn walk_dir(path: &PathBuf) -> io::Result<Vec<PathBuf>> {
use crate::configuration::CONFIG;
pub fn walk_dir(path: &PathBuf) -> Result<Vec<PathBuf>, &str> {
let mut types_builder = TypesBuilder::new();
types_builder.add_defaults();
@ -14,7 +16,19 @@ pub fn walk_dir(path: &PathBuf) -> io::Result<Vec<PathBuf>> {
types_builder.select("sound");
let entries: Vec<PathBuf> = WalkBuilder::new(path)
let search_path = CONFIG.base_path.join(path);
eprintln!(
"DEBUGPRINT[1]: file_explorer.rs:19: search_path={:#?}",
search_path
);
// PathBuf.join() can override the hole path, this ensure we're not accessing files outside
// base_dir
if !search_path.starts_with(&CONFIG.base_path) {
return Err("Tried to access file or directory outside of server `base_dir` config.");
}
let entries: Vec<PathBuf> = WalkBuilder::new(search_path)
.types(types_builder.build().unwrap())
.build()
.filter_map(|entry| entry.ok())