aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/assets.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-11-30 12:32:44 +0100
committermetamuffin <metamuffin@disroot.org>2025-11-30 12:32:44 +0100
commit8174d129fbabd2d39323678d11d868893ddb429a (patch)
tree7979a528114cd5fb827f748f678a916e8e8eeddc /server/src/ui/assets.rs
parent5db15c323d76dca9ae71b0204d63dcb09fbbcbc5 (diff)
downloadjellything-8174d129fbabd2d39323678d11d868893ddb429a.tar
jellything-8174d129fbabd2d39323678d11d868893ddb429a.tar.bz2
jellything-8174d129fbabd2d39323678d11d868893ddb429a.tar.zst
new sync cache
Diffstat (limited to 'server/src/ui/assets.rs')
-rw-r--r--server/src/ui/assets.rs118
1 files changed, 36 insertions, 82 deletions
diff --git a/server/src/ui/assets.rs b/server/src/ui/assets.rs
index 969f3ed..d7663c3 100644
--- a/server/src/ui/assets.rs
+++ b/server/src/ui/assets.rs
@@ -4,108 +4,62 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use super::error::MyResult;
-use crate::{
- helper::{cache::CacheControlFile, A},
- CONF,
-};
-use anyhow::{anyhow, bail, Context};
-use jellycommon::NodeID;
-use jellylogic::session::Session;
-use log::info;
+use crate::helper::{cache::CacheControlImage, A};
+use anyhow::{anyhow, Context};
+use jellycache::{CacheContentType, CacheKey};
+use jellycommon::{api::NodeFilterSort, NodeID, Picture, PictureSlot};
+use jellylogic::{assets::get_node_thumbnail, node::get_node, session::Session};
use rocket::{get, http::ContentType, response::Redirect};
-use std::path::PathBuf;
+use std::str::FromStr;
pub const AVIF_QUALITY: f32 = 50.;
pub const AVIF_SPEED: u8 = 5;
-#[get("/asset/<token>?<width>")]
-pub async fn r_asset(
+#[get("/image/<key>?<size>")]
+pub async fn r_image(
_session: A<Session>,
- token: &str,
- width: Option<usize>,
-) -> MyResult<(ContentType, CacheControlFile)> {
- // let width = width.unwrap_or(2048);
- // let asset = AssetInner::deser(token)?;
+ key: A<Picture>,
+ size: Option<usize>,
+) -> MyResult<(ContentType, CacheControlImage)> {
+ let size = size.unwrap_or(2048);
- // // if let AssetInner::Federated { host, asset } = asset {
- // // let session = fed.get_session(&host).await?;
+ let key = CacheKey(key.0 .0);
+ if !matches!(key.content_type(), CacheContentType::Image) {
+ Err(anyhow!("request to non-image"))?
+ }
- // // 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 path = {
- // 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(size.clamp(128, 2048).ilog2());
+ let encoded = jellytranscoder::image::transcode(key, AVIF_QUALITY, AVIF_SPEED, width)
+ .context("transcoding asset")?;
- // // fit the resolution into a finite set so the maximum cache is finite too.
- // let width = 2usize.pow(width.clamp(128, 2048).ilog2());
- // jellytranscoder::image::transcode(&source, AVIF_QUALITY, AVIF_SPEED, width)
- // .await
- // .context("transcoding asset")?
- // };
- // info!("loading asset from {path:?}");
- // Ok((
- // ContentType::AVIF,
- // CacheControlFile::new_cachekey(&path.abs()).await?,
- // ))
- todo!()
+ Ok((ContentType::AVIF, CacheControlImage(encoded)))
}
-// pub async fn resolve_asset(asset: AssetInner) -> anyhow::Result<PathBuf> {
-// match asset {
-// AssetInner::Cache(c) => Ok(c.abs()),
-// AssetInner::Assets(c) => Ok(CONF.asset_path.join(c)),
-// AssetInner::Media(c) => Ok(c),
-// _ => bail!("wrong asset type"),
-// }
-// }
-
-#[get("/n/<id>/poster?<width>")]
+#[get("/n/<id>/image/<slot>?<size>")]
pub async fn r_item_poster(
session: A<Session>,
id: A<NodeID>,
- width: Option<usize>,
-) -> MyResult<Redirect> {
- // let asset = get_node_poster(&session.0, id.0)?;
- // Ok(Redirect::permanent(rocket::uri!(r_asset(asset.0, width))))
- Err(anyhow!("a").into())
-}
-
-#[get("/n/<id>/backdrop?<width>")]
-pub async fn r_item_backdrop(
- session: A<Session>,
- id: A<NodeID>,
- width: Option<usize>,
-) -> MyResult<Redirect> {
- // let asset = get_node_backdrop(&session.0, id.0)?;
- // Ok(Redirect::permanent(rocket::uri!(r_asset(asset.0, width))))
- Err(anyhow!("a").into())
-}
-
-#[get("/n/<id>/person/<index>/asset?<group>&<width>")]
-pub async fn r_person_asset(
- session: A<Session>,
- id: A<NodeID>,
- index: usize,
- group: String,
- width: Option<usize>,
+ slot: &str,
+ size: Option<usize>,
) -> MyResult<Redirect> {
- // let group = PeopleGroup::from_str_opt(&group).ok_or(anyhow!("unknown people group"))?;
- // let asset = get_node_person_asset(&session.0, id.0, group, index)?;
- // Ok(Redirect::permanent(rocket::uri!(r_asset(asset.0, width))))
- Err(anyhow!("a").into())
+ let slot = PictureSlot::from_str(slot).map_err(|_| anyhow!("slot invalid"))?;
+ let node = get_node(&session.0, id.0, false, false, NodeFilterSort::default())?;
+ let picture = node
+ .node
+ .pictures
+ .get(&slot)
+ .ok_or(anyhow!("no pic todo"))?;
+ Ok(Redirect::permanent(rocket::uri!(r_image(*picture, size))))
}
-#[get("/n/<id>/thumbnail?<t>&<width>")]
+#[get("/n/<id>/thumbnail?<t>&<size>")]
pub async fn r_node_thumbnail(
session: A<Session>,
id: A<NodeID>,
t: f64,
- width: Option<usize>,
+ size: Option<usize>,
) -> MyResult<Redirect> {
- // let asset = get_node_thumbnail(&session.0, id.0, t).await?;
- // Ok(Redirect::temporary(rocket::uri!(r_asset(asset.0, width))))
- Err(anyhow!("a").into())
+ let picture = get_node_thumbnail(&session.0, id.0, t).await?;
+ Ok(Redirect::permanent(rocket::uri!(r_image(picture, size))))
}