From 8174d129fbabd2d39323678d11d868893ddb429a Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 30 Nov 2025 12:32:44 +0100 Subject: new sync cache --- common/Cargo.toml | 1 + common/src/helpers.rs | 41 ++++++++++++++++++++++++++++++++++++++++- common/src/lib.rs | 9 ++++----- common/src/routes.rs | 9 +++------ 4 files changed, 48 insertions(+), 12 deletions(-) (limited to 'common') diff --git a/common/Cargo.toml b/common/Cargo.toml index 37e9c6c..7e25933 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -9,3 +9,4 @@ chrono = { version = "0.4.42", features = ["serde"] } blake3 = "1.8.2" hex = "0.4.3" jellystream-types = { path = "../stream/types" } +base64 = "0.22.1" diff --git a/common/src/helpers.rs b/common/src/helpers.rs index db75ba9..431bf8c 100644 --- a/common/src/helpers.rs +++ b/common/src/helpers.rs @@ -4,7 +4,9 @@ Copyright (C) 2025 metamuffin */ -use crate::{CreditCategory, IdentifierType}; +use base64::{Engine, prelude::BASE64_URL_SAFE}; + +use crate::{CreditCategory, IdentifierType, Picture, PictureSlot}; use std::{fmt::Display, ops::Deref, str::FromStr}; #[derive(PartialEq)] @@ -129,3 +131,40 @@ impl FromStr for IdentifierType { }) } } +impl Display for PictureSlot { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + PictureSlot::Backdrop => "backdrop", + PictureSlot::Cover => "cover", + }) + } +} +impl FromStr for PictureSlot { + type Err = (); + fn from_str(s: &str) -> Result { + Ok(match s { + "backdrop" => PictureSlot::Backdrop, + "cover" => PictureSlot::Cover, + _ => return Err(()), + }) + } +} + +impl Display for Picture { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&BASE64_URL_SAFE.encode(self.0)) + } +} +impl FromStr for Picture { + type Err = &'static str; + fn from_str(s: &str) -> Result { + let mut out = [0; 32]; + let size = BASE64_URL_SAFE + .decode_slice(s, &mut out) + .map_err(|_| "invalid base64 picture key")?; + if size != out.len() { + return Err("picture key parse invalid size"); + } + Ok(Self(out)) + } +} diff --git a/common/src/lib.rs b/common/src/lib.rs index f3d8416..3f535fd 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -68,7 +68,7 @@ pub struct Node { pub federated: Option, pub tags: BTreeSet, pub ratings: BTreeMap, - pub pictures: BTreeMap, + pub pictures: BTreeMap, pub credits: BTreeMap>, pub identifiers: BTreeMap, pub visibility: Visibility, @@ -78,10 +78,8 @@ pub struct Node { #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] #[serde(rename_all = "snake_case")] pub enum PictureSlot { - Poster, Cover, Backdrop, - Headshot, } #[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] @@ -105,8 +103,9 @@ pub enum IdentifierType { Omdb, } -#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] -pub struct Asset(pub PathBuf); +// TODO custom b64 ser +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] +pub struct Picture(pub [u8; 32]); #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Appearance { diff --git a/common/src/routes.rs b/common/src/routes.rs index f78c7f0..19b0206 100644 --- a/common/src/routes.rs +++ b/common/src/routes.rs @@ -3,7 +3,7 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin */ -use crate::{api::NodeFilterSort, user::ApiWatchedState, NodeID, CreditCategory}; +use crate::{CreditCategory, NodeID, PictureSlot, api::NodeFilterSort, user::ApiWatchedState}; pub fn u_home() -> String { "/home".to_owned() @@ -20,11 +20,8 @@ pub fn u_node_slug_player(node: &str) -> String { pub fn u_node_slug_player_time(node: &str, time: f64) -> String { format!("/n/{node}/player?t={time}") } -pub fn u_node_slug_poster(node: &str, width: usize) -> String { - format!("/n/{node}/poster?width={width}") -} -pub fn u_node_slug_backdrop(node: &str, width: usize) -> String { - format!("/n/{node}/backdrop?width={width}") +pub fn u_node_image(node: &str, slot: PictureSlot, width: usize) -> String { + format!("/n/{node}/image/{slot}?width={width}") } pub fn u_node_slug_watched(node: &str, state: ApiWatchedState) -> String { format!("/n/{node}/watched?state={state}") -- cgit v1.3