aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/assets.rs
blob: 8f3fb4a88dbe339ca670c2d9c9f36dbc04a1ffb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
    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) 2026 metamuffin <metamuffin.org>
*/
use super::error::MyResult;
use anyhow::{Context, anyhow};
use rocket::{get, http::ContentType, response::Redirect};
use std::str::FromStr;

pub const AVIF_QUALITY: u32 = 70;
pub const AVIF_SPEED: u8 = 5;

#[get("/image/<key..>?<size>")]
pub async fn r_image(
    _session: A<Session>,
    key: A<Asset>,
    size: Option<usize>,
) -> MyResult<(ContentType, CacheControlImage)> {
    let size = size.unwrap_or(2048);

    if !key.0.0.ends_with(".image") {
        Err(anyhow!("request to non-image"))?
    }

    // 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.0.0, AVIF_QUALITY, AVIF_SPEED, width)
        .context("transcoding asset")?;

    Ok((ContentType::AVIF, CacheControlImage(encoded)))
}

#[get("/n/<id>/image/<slot>?<size>")]
pub async fn r_item_poster(
    session: A<Session>,
    id: A<NodeID>,
    slot: &str,
    size: Option<usize>,
) -> MyResult<Redirect> {
    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)
        .cloned()
        .ok_or(anyhow!("no pic todo"))?;
    Ok(Redirect::permanent(rocket::uri!(r_image(picture, size))))
}

#[get("/n/<id>/thumbnail?<t>&<size>")]
pub async fn r_node_thumbnail(
    session: A<Session>,
    id: A<NodeID>,
    t: f64,
    size: Option<usize>,
) -> MyResult<Redirect> {
    let picture = get_node_thumbnail(&session.0, id.0, t).await?;
    Ok(Redirect::permanent(rocket::uri!(r_image(picture, size))))
}