diff options
| author | metamuffin <metamuffin@disroot.org> | 2023-07-31 19:53:01 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2023-07-31 19:53:01 +0200 | 
| commit | aeafba7847e189313df3025e6d6f291999b57350 (patch) | |
| tree | bf7affdca28208695648bc9b18856cbb7049d1e8 /server/src/routes/ui/assets.rs | |
| parent | 0c651f11920350a4aa96aa24f8fe15b28390aed2 (diff) | |
| download | jellything-aeafba7847e189313df3025e6d6f291999b57350.tar jellything-aeafba7847e189313df3025e6d6f291999b57350.tar.bz2 jellything-aeafba7847e189313df3025e6d6f291999b57350.tar.zst | |
update server to new schema
Diffstat (limited to 'server/src/routes/ui/assets.rs')
| -rw-r--r-- | server/src/routes/ui/assets.rs | 44 | 
1 files changed, 44 insertions, 0 deletions
| diff --git a/server/src/routes/ui/assets.rs b/server/src/routes/ui/assets.rs new file mode 100644 index 0000000..2c0b85a --- /dev/null +++ b/server/src/routes/ui/assets.rs @@ -0,0 +1,44 @@ +/* +    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 <metamuffin.org> +*/ +use crate::{ +    database::Database, +    routes::ui::{account::session::Session, error::MyError, CacheControlFile}, +    CONF, +}; +use anyhow::anyhow; +use log::info; +use rocket::{get, http::ContentType, FromFormField, State, UriDisplayQuery}; +use std::{path::PathBuf, str::FromStr}; +use tokio::fs::File; + +#[derive(FromFormField, UriDisplayQuery)] +pub enum AssetRole { +    Poster, +    Backdrop, +} + +#[get("/item_assets/<id>?<role>")] +pub async fn r_item_assets( +    _sess: Session, +    id: String, +    role: AssetRole, +    db: &State<Database>, +) -> Result<(ContentType, CacheControlFile), MyError> { +    let node = db.node.get(&id)?.ok_or(anyhow!("node does not exist"))?; +    let path = CONF.asset_path.join( +        match role { +            AssetRole::Backdrop => node.private.backdrop, +            AssetRole::Poster => node.private.poster, +        } +        .unwrap_or_else(|| PathBuf::from_str("fallback.jpeg").unwrap()), +    ); +    info!("loading asset from {path:?}"); +    let ext = path.extension().unwrap().to_str().unwrap(); +    Ok(( +        ContentType::from_extension(ext).unwrap(), +        CacheControlFile::new(File::open(path).await?).await, +    )) +} | 
