/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ use crate::{ database::Database, routes::ui::{account::session::Session, error::MyError, CacheControlFile}, }; use anyhow::{anyhow, Context}; use jellybase::{permission::NodePermissionExt, AssetLocationExt}; use jellycommon::AssetLocation; pub use jellycommon::AssetRole; use log::info; use rocket::{get, http::ContentType, State}; use std::{path::PathBuf, str::FromStr}; use tokio::fs::File; #[get("/n//asset?&")] pub async fn r_item_assets( session: Session, db: &State, id: &str, role: AssetRole, width: Option, ) -> Result<(ContentType, CacheControlFile), MyError> { let node = db .node .get(&id.to_string())? .only_if_permitted(&session.user.permissions) .ok_or(anyhow!("node does not exist"))?; let mut asset = match role { AssetRole::Backdrop => node.private.backdrop, AssetRole::Poster => node.private.poster, }; if let None = asset { if let Some(parent) = &node.public.path.last() { let parent = db.node.get(parent)?.ok_or(anyhow!("node does not exist"))?; asset = match role { AssetRole::Backdrop => parent.private.backdrop, AssetRole::Poster => parent.private.poster, }; } }; let asset = asset.unwrap_or(AssetLocation::Assets( PathBuf::from_str("fallback.avif").unwrap(), )); // fit the resolution into a finite set so the maximum cache is finite too. let width = 2usize.pow(width.unwrap_or(2048).clamp(128, 8196).ilog2()); let path = jellytranscoder::image::transcode(asset, 50., 5, width) .await .context("transcoding asset")?; info!("loading asset from {path:?}"); Ok(( ContentType::AVIF, CacheControlFile::new(File::open(path.path()).await.context("opening file")?).await, )) }