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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
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 super::error::MyError;
use super::player::player_uri;
use crate::uri;
use crate::{
library::{Directory, Item, Library, Node},
routes::ui::{
account::session::Session,
layout::{DynLayoutPage, LayoutPage},
},
};
use anyhow::Context;
use jellycommon::DirectoryKind;
use log::info;
use rocket::{get, http::ContentType, State};
use rocket::{FromFormField, UriDisplayQuery};
use std::{path::PathBuf, sync::Arc};
use tokio::fs::File;
#[get("/library/<path..>")]
pub async fn r_library_node(
_sess: Session,
path: PathBuf,
library: &State<Library>,
) -> Result<DynLayoutPage<'_>, MyError> {
let node = library
.nested_path(&path)
.context("retrieving library node")?;
Ok(LayoutPage {
title: node.common().title.to_string(),
show_back: node.get_item().is_ok(),
content: markup::new! {
@NodePage { node: &node }
},
..Default::default()
})
}
markup::define! {
NodePage<'a>(node: &'a Arc<Node>) {
@match node.as_ref() {
Node::Directory(dir) => { @match dir.info.kind {
DirectoryKind::Series => { @SeriesPage { dir } }
_ => { @DirectoryPage { dir } }
} }
Node::Item(item) => { @ItemPage { item } }
}
}
NodeCard<'a>(node: &'a Arc<Node>) {
@match node.as_ref() {
Node::Directory(dir) => {@PosterCard {
wide: !matches!(dir.info.kind, DirectoryKind::Series | DirectoryKind::Season),
dir: true,
path: dir.lib_path.clone(),
title: &dir.info.title
}}
Node::Item(item) => {@PosterCard {
wide: false, dir: false,
path: item.lib_path.clone(),
title: &item.info.title
}}
}
}
DirectoryPage<'a>(dir: &'a Arc<Directory>) {
div.page.dir {
h1 { @dir.info.title }
@if let Some(parent) = dir.lib_path.parent() {
a.dirup[href=uri!(r_library_node(&parent))] { "Go up" }
}
ul.directorylisting {
@for node in &dir.children {
li { @NodeCard { node } }
}
}
}
}
PosterCard<'a>(path: PathBuf, title: &'a str, wide: bool, dir: bool) {
div[class=if *wide {"card wide poster"} else {"card poster"}] {
div.banner {
a[href=uri!(r_library_node(path))] {
img[src=uri!(r_item_assets(path, AssetRole::Poster))];
}
@if *dir {
div.hoverdir { a[href=&uri!(r_library_node(path))] { "Open" } }
} else {
div.hoveritem { a[href=&player_uri(path)] { "▶" } }
}
}
p.title {
a[href=uri!(r_library_node(path))] {
@title
}
}
}
}
ItemPage<'a>(item: &'a Arc<Item>) {
// TODO different image here
img.backdrop[src=uri!(r_item_assets(&item.lib_path, AssetRole::Backdrop))];
div.page.item {
div.banner {
img[src=uri!(r_item_assets(&item.lib_path, AssetRole::Poster))];
}
div.title {
h1 { @item.info.title }
// TODO release date, duration, ratings
a.play[href=&player_uri(&item.lib_path)] { "Watch now" }
}
div.details {
h3 { @item.info.tagline }
p { @item.info.description }
}
}
}
SeriesPage<'a>(dir: &'a Arc<Directory>) {
// TODO different image here
img.backdrop[src=uri!(r_item_assets(&dir.lib_path, AssetRole::Backdrop))];
div.page.item {
div.banner {
img[src=uri!(r_item_assets(&dir.lib_path, AssetRole::Poster))];
}
div.title {
h1 { @dir.info.title }
}
div.details {
h3 { @dir.info.tagline }
p { @dir.info.description }
}
ol { @for ep in &dir.children {
li { a[href=uri!(r_library_node(ep.lib_path()))] { @ep.common().title } }
} }
}
}
}
#[derive(FromFormField, UriDisplayQuery)]
pub enum AssetRole {
Poster,
Backdrop,
}
#[get("/item_assets/<path..>?<role>")]
pub async fn r_item_assets(
_sess: Session,
path: PathBuf,
role: AssetRole,
library: &State<Library>,
) -> Result<(ContentType, File), MyError> {
let node = library
.nested_path(&path)
.context("retrieving library node")?;
let path = node.get_asset(library, role);
info!("loading asset from {path:?}");
let ext = path.extension().unwrap().to_str().unwrap();
Ok((
ContentType::from_extension(ext).unwrap(),
File::open(path).await?,
))
}
|