diff --git a/src/public.rs b/src/public.rs index 862123c..6ba1fb8 100644 --- a/src/public.rs +++ b/src/public.rs @@ -74,6 +74,9 @@ impl Public { Ok((t, b)) } + /// Build URI for given `path` + /// + /// * result requires URL encode pub fn href(&self, info_hash: &str, path: &str) -> Option { let mut relative = PathBuf::from(info_hash); relative.push(path); @@ -89,6 +92,21 @@ impl Public { } } + /// Return canonical absolute path + /// + /// * `None` if the given URI does not exist or has denied location + pub fn path(&self, relative: &str) -> Option { + let mut p = PathBuf::from(&self.root); + p.push(&relative); + + let path = p.canonicalize().ok()?; + if path.starts_with(&self.root) && c.exists() { + Some(path) + } else { + None + } + } + // Helpers fn files(