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 --- server/src/routes/ui/assets.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'server') 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!(), } } -- cgit v1.2.3-70-g09d2