From 853a0022084a503b4747dfadd4e217b950ec6d8d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 10 Apr 2024 14:35:15 +0200 Subject: dont transcode federated assets --- base/src/assetfed.rs | 8 ++++++++ server/src/routes/ui/assets.rs | 40 +++++++++++++++++++++------------------- web/script/player/mod.ts | 2 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/base/src/assetfed.rs b/base/src/assetfed.rs index e7c0124..4ca587b 100644 --- a/base/src/assetfed.rs +++ b/base/src/assetfed.rs @@ -69,4 +69,12 @@ impl AssetInner { .context("asset token has invalid format")?; Ok(data) } + + /// Returns `true` if the asset inner is [`Federated`]. + /// + /// [`Federated`]: AssetInner::Federated + #[must_use] + pub fn is_federated(&self) -> bool { + matches!(self, Self::Federated { .. }) + } } diff --git a/server/src/routes/ui/assets.rs b/server/src/routes/ui/assets.rs index 5b9f70e..115c02a 100644 --- a/server/src/routes/ui/assets.rs +++ b/server/src/routes/ui/assets.rs @@ -27,16 +27,27 @@ pub async fn r_asset( token: &str, width: Option, ) -> MyResult<(ContentType, CacheControlFile)> { + let width = width.unwrap_or(2048); let asset = AssetInner::deser(token)?; - let source = resolve_asset(asset, fed).await.context("resolving asset")?; - - // fit the resolution into a finite set so the maximum cache is finite too. - let width = 2usize.pow(width.unwrap_or(2048).clamp(128, 2048).ilog2()); - // TODO configure avif quality and speed. - let path = jellytranscoder::image::transcode(source, 50., 5, width) - .await - .context("transcoding asset")?; + let path = if let AssetInner::Federated { host, asset } = asset { + let session = fed.get_session(&host).await?; + + let asset = base64::engine::general_purpose::URL_SAFE.encode(asset); + async_cache_file(&["fed-asset", &asset], |out| async { + session.asset(out, &asset, width).await + }) + .await? + } else { + let source = resolve_asset(asset).await.context("resolving asset")?; + + // fit the resolution into a finite set so the maximum cache is finite too. + let width = 2usize.pow(width.clamp(128, 2048).ilog2()); + // TODO configure avif quality and speed. + jellytranscoder::image::transcode(source, 50., 5, width) + .await + .context("transcoding asset")? + }; info!("loading asset from {path:?}"); Ok(( ContentType::AVIF, @@ -44,21 +55,12 @@ pub async fn r_asset( )) } -pub async fn resolve_asset(asset: AssetInner, fed: &State) -> anyhow::Result { +pub async fn resolve_asset(asset: AssetInner) -> anyhow::Result { match asset { - AssetInner::Federated { host, asset } => { - let session = fed.get_session(&host).await?; - - let asset = base64::engine::general_purpose::URL_SAFE.encode(asset); - Ok(async_cache_file(&["fed-asset", &asset], |out| async { - session.asset(out, &asset, 2048).await - }) - .await? - .abs()) - } AssetInner::Cache(c) => Ok(c.abs()), AssetInner::Assets(c) => Ok(CONF.asset_path.join(c)), AssetInner::Library(c) => Ok(CONF.library_path.join(c)), + _ => unreachable!(), } } diff --git a/web/script/player/mod.ts b/web/script/player/mod.ts index 3774c62..59aae99 100644 --- a/web/script/player/mod.ts +++ b/web/script/player/mod.ts @@ -310,7 +310,7 @@ function send_player_progress(node_id: string, player: Player) { function mouse_idle(e: HTMLElement, timeout: number): OVar { let ct: number; const idle = new OVar(false) - e.onmouseleave = () => { + e.onmouseleave = () => { clearTimeout(ct) } e.onmousemove = () => { -- cgit v1.2.3-70-g09d2