004 Multi-library Support #54

Merged
cameron merged 19 commits from 004-multi-library into master 2026-04-21 01:55:23 +00:00
Showing only changes of commit b04dd8b601 - Show all commits

View File

@@ -1,6 +1,6 @@
use ::anyhow;
use actix::{Handler, Message};
use anyhow::{Context, anyhow};
use anyhow::Context;
use std::collections::HashSet;
use std::fmt::Debug;
use std::fs::read_dir;
@@ -1024,33 +1024,58 @@ pub fn is_valid_full_path<P: AsRef<Path> + Debug + AsRef<std::ffi::OsStr>>(
match is_path_above_base_dir(base, &mut path, new_file) {
Ok(path) => Some(path),
Err(e) => {
Err(PathValidationError::DoesNotExist(p)) => {
debug!("Path does not exist under base {:?}: {:?}", base, p);
None
}
Err(PathValidationError::AboveBase(p)) => {
error!("Path above base directory {:?}: {:?}", base, p);
None
}
Err(PathValidationError::Other(e)) => {
error!("{}", e);
None
}
}
}
#[derive(Debug)]
enum PathValidationError {
DoesNotExist(PathBuf),
AboveBase(PathBuf),
Other(anyhow::Error),
}
impl std::fmt::Display for PathValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PathValidationError::DoesNotExist(p) => write!(f, "Path does not exist: {:?}", p),
PathValidationError::AboveBase(p) => write!(f, "Path above base directory: {:?}", p),
PathValidationError::Other(e) => write!(f, "{}", e),
}
}
}
fn is_path_above_base_dir<P: AsRef<Path> + Debug>(
base: P,
full_path: &mut PathBuf,
new_file: bool,
) -> anyhow::Result<PathBuf> {
full_path
.absolutize()
.with_context(|| format!("Unable to resolve absolute path: {:?}", full_path))
.map_or_else(
|e| Err(anyhow!(e)),
|p| {
if p.starts_with(base) && (new_file || p.exists()) {
Ok(p.into_owned())
} else if !p.exists() {
Err(anyhow!("Path does not exist: {:?}", p))
} else {
Err(anyhow!("Path above base directory"))
}
},
)
) -> Result<PathBuf, PathValidationError> {
match full_path.absolutize() {
Err(e) => Err(PathValidationError::Other(
anyhow::Error::new(e)
.context(format!("Unable to resolve absolute path: {:?}", full_path)),
)),
Ok(p) => {
if p.starts_with(base) && (new_file || p.exists()) {
Ok(p.into_owned())
} else if !p.exists() {
Err(PathValidationError::DoesNotExist(p.into_owned()))
} else {
Err(PathValidationError::AboveBase(p.into_owned()))
}
}
}
}
/// Handler for GPS summary endpoint
@@ -1289,6 +1314,7 @@ impl Handler<RefreshThumbnailsMessage> for StreamActor {
mod tests {
use super::*;
use crate::database::DbError;
use ::anyhow::anyhow;
use std::collections::HashMap;
use std::env;
use std::fs::File;