use log::info;
use std::collections::HashMap;
use std::ffi::OsString;
use std::path::Path;
use std::sync::Mutex;
use std::{path::PathBuf, sync::Arc};
use walkdir::WalkDir;
#[derive(Clone, Debug)]
pub(crate) struct WikilinkIndex {
filenames: Arc<Mutex<HashMap<OsString, PathBuf>>>,
local_base: PathBuf,
}
impl WikilinkIndex {
pub(crate) fn new(local_base: PathBuf) -> Self {
let index = Self {
local_base,
filenames: Arc::new(Mutex::new(HashMap::new())),
};
index.start_indexing();
index
}
pub(crate) fn start_indexing(&self) {
info!(
"Starting file indexing for wikilinks in {}",
self.local_base.display()
);
for entry in WalkDir::new(&self.local_base)
.follow_links(false)
.into_iter()
.filter_map(Result::ok)
{
if let Some(filename) = entry.path().file_name() {
self.filenames
.lock()
.unwrap()
.insert(filename.to_os_string(), entry.path().to_path_buf());
}
}
}
pub(crate) fn contains_path(&self, path: &Path) -> Option<PathBuf> {
self.filenames
.lock()
.unwrap()
.get(path.file_name()?)
.cloned()
}
}