Add function for path conversion with fallback

This commit is contained in:
2025-10-05 17:16:00 -04:00
parent 70e1c565ad
commit df636a1ef2

View File

@@ -1,4 +1,5 @@
/// Generates random paths for storing assets
use log::*;
use rand::{rng, Rng};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
@@ -103,3 +104,15 @@ pub fn to_web_path<P: AsRef<Path>>(path: P, asset_type: AssetType) -> BackendRes
Ok(url_path.path().to_string())
}
/// Convert a server-side image path to a web path,
/// or fall back to a placeholder if it fails or the `path` is `None`,
/// If path conversion fails, the error is logged as a "warning".
pub fn to_web_path_or_placeholder<P: AsRef<Path>>(path: Option<P>) -> String {
path.and_then(|path| {
to_web_path(path, AssetType::Image)
.inspect_err(|e| warn!("{e}"))
.ok()
})
.unwrap_or(MUSIC_PLACEHOLDER_WEB_PATH.to_string())
}