//! Filesystem functions and structures //! //! Code related to safe handling of paths form requests and reading data //! from the filesystem. //! //! In order to get a safe path, that the top-level functions in this module //! want, call [`RequestPath::verify_safe()`] mod request_path; use camino::Utf8PathBuf; pub use request_path::{Mime, Mimetype, RequestPath, SafePath, SafePathBuf}; use tokio::fs::File; use crate::RuntimeError; pub async fn open_file<'p>(path: SafePath<'p>) -> Result { File::open(*path) .await .map_err(|err| RuntimeError::file_error(err, path.to_path_buf())) } pub async fn read_file<'p>(path: SafePath<'p>) -> Result, RuntimeError> { let system_path = *path; unsafe_read_file(system_path.to_owned()).await } pub async fn unsafe_read_file(path: Utf8PathBuf) -> Result, RuntimeError> { match tokio::fs::read(&path).await { Err(err) => Err(RuntimeError::file_error(err, path)), Ok(vec) => Ok(vec), } } pub async fn read_file_to_string<'p>(path: SafePath<'p>) -> Result { let system_path = *path; unsafe_read_file_to_string(system_path.to_path_buf()).await } pub async fn unsafe_read_file_to_string(path: Utf8PathBuf) -> Result { match tokio::fs::read_to_string(&path).await { Err(err) => Err(RuntimeError::file_error(err, path)), Ok(str) => Ok(str), } }