From 9898998344f9897d59b629ce643a0e243f5e7b09 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 3 Aug 2023 15:36:34 +0200 Subject: federated assets and refactored asset paths --- server/src/routes/ui/assets.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'server/src/routes/ui') diff --git a/server/src/routes/ui/assets.rs b/server/src/routes/ui/assets.rs index 7fa083c..4b5c64a 100644 --- a/server/src/routes/ui/assets.rs +++ b/server/src/routes/ui/assets.rs @@ -9,6 +9,7 @@ use crate::{ CONF, }; use anyhow::anyhow; +use jellycommon::AssetLocation; use log::info; use rocket::{get, http::ContentType, FromFormField, State, UriDisplayQuery}; use std::{path::PathBuf, str::FromStr}; @@ -16,7 +17,9 @@ use tokio::fs::File; #[derive(FromFormField, UriDisplayQuery)] pub enum AssetRole { + #[field(value = "poster")] Poster, + #[field(value = "backdrop")] Backdrop, } @@ -32,15 +35,31 @@ pub async fn r_item_assets( AssetRole::Backdrop => node.private.backdrop, AssetRole::Poster => node.private.poster, } - .map(|e| CONF.library_path.join(e)) + .map(|e| e.path()) .unwrap_or_else(|| { CONF.asset_path .join(PathBuf::from_str("fallback.jpeg").unwrap()) }); info!("loading asset from {path:?}"); - let ext = path.extension().unwrap().to_str().unwrap(); + let ext = path + .extension() + .map(|e| e.to_str().unwrap()) + .unwrap_or("jpeg"); Ok(( ContentType::from_extension(ext).unwrap(), CacheControlFile::new(File::open(path).await?).await, )) } + +pub trait AssetLocationExt { + fn path(&self) -> PathBuf; +} +impl AssetLocationExt for AssetLocation { + fn path(&self) -> PathBuf { + match self { + AssetLocation::Assets(p) => CONF.asset_path.join(p), + AssetLocation::Cache(p) => CONF.cache_path.join(p), + AssetLocation::Library(p) => CONF.library_path.join(p), + } + } +} -- cgit v1.2.3-70-g09d2