148 lines
5.2 KiB
Rust
148 lines
5.2 KiB
Rust
//! Local demo: two jellymove agents ("cloud" on :8097, "home" on :8098), each
|
|
//! with a fake Jellyfin and some throwaway media in a temp folder.
|
|
//!
|
|
//! npm --prefix ui ci && npm --prefix ui run build
|
|
//! cargo run -p jm-server --example demo
|
|
//!
|
|
//! Then open http://127.0.0.1:8097 and move something to "home".
|
|
|
|
#[path = "../tests/fake_jellyfin/mod.rs"]
|
|
#[allow(dead_code)]
|
|
mod fake_jellyfin;
|
|
|
|
use std::path::Path;
|
|
use std::sync::Arc;
|
|
|
|
use fake_jellyfin::{Fake, Lib};
|
|
use jm_server::config::{
|
|
Config, HooksConfig, JellyfinConfig, PeerConfig, TransferConfig, UiConfig,
|
|
};
|
|
|
|
fn write(path: &Path, len: usize) {
|
|
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
let data: Vec<u8> = (0..len as u32)
|
|
.map(|i| (i.wrapping_mul(2654435761) >> 11) as u8)
|
|
.collect();
|
|
std::fs::write(path, data).unwrap();
|
|
}
|
|
|
|
fn libs(root: &Path) -> Vec<Lib> {
|
|
vec![
|
|
Lib {
|
|
name: "Movies".into(),
|
|
collection_type: "movies",
|
|
location: root.join("movies"),
|
|
},
|
|
Lib {
|
|
name: "TV Shows".into(),
|
|
collection_type: "tvshows",
|
|
location: root.join("tv"),
|
|
},
|
|
]
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
tracing_subscriber_init();
|
|
// One fixed folder, emptied on each start: a temp folder per run would
|
|
// be left behind (hundreds of MB) whenever the demo is killed.
|
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/demo");
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(&root)?;
|
|
let root = root.canonicalize()?;
|
|
let cloud = root.join("cloud");
|
|
let home = root.join("home");
|
|
for r in [&cloud, &home] {
|
|
std::fs::create_dir_all(r.join("movies"))?;
|
|
std::fs::create_dir_all(r.join("tv"))?;
|
|
}
|
|
const MB: usize = 1024 * 1024;
|
|
for (folder, size) in [
|
|
("Arrival (2016)", 180 * MB),
|
|
("Blade Runner (1982)", 120 * MB),
|
|
("Heat (1995)", 60 * MB),
|
|
] {
|
|
let base = cloud.join("movies").join(folder);
|
|
write(&base.join(format!("{folder}.mkv")), size);
|
|
write(&base.join(format!("{folder}.nfo")), 300);
|
|
write(&base.join(format!("{folder}.en.srt")), 40_000);
|
|
write(&base.join("poster.jpg"), 200_000);
|
|
}
|
|
for (season, eps) in [(1, 6), (2, 4)] {
|
|
for ep in 1..=eps {
|
|
write(
|
|
&cloud.join(format!(
|
|
"tv/Severance/Season {season:02}/Severance S{season:02}E{ep:02}.mkv"
|
|
)),
|
|
20 * MB,
|
|
);
|
|
}
|
|
}
|
|
write(&cloud.join("tv/Severance/tvshow.nfo"), 2_000);
|
|
write(&cloud.join("tv/Severance/poster.jpg"), 300_000);
|
|
write(
|
|
&home.join("movies/The Thing (1982)/The Thing (1982).mkv"),
|
|
40 * MB,
|
|
);
|
|
|
|
let fake_cloud = Fake::new(libs(&cloud));
|
|
let fake_home = Fake::new(libs(&home));
|
|
// Bob only has an account on cloud, so his watch history can't follow a
|
|
// move to home until one is created there.
|
|
fake_cloud.add_user("u2", "Bob");
|
|
if let Some(id) = fake_cloud.find("Movie", "Arrival") {
|
|
fake_cloud.set_user_data_for("u2", &id, serde_json::json!({"PlaybackPositionTicks": 36_000_000_000i64}));
|
|
fake_cloud.set_user_data(&id, serde_json::json!({"Played": true, "PlayCount": 1}));
|
|
}
|
|
let jf_cloud = fake_cloud.serve().await;
|
|
let jf_home = fake_home.serve().await;
|
|
|
|
let mut tasks = Vec::new();
|
|
for (name, root, jf, port, peer_port) in [
|
|
("cloud", &cloud, jf_cloud, 8097, 8098),
|
|
("home", &home, jf_home, 8098, 8097),
|
|
] {
|
|
let cfg = Config {
|
|
node_name: name.into(),
|
|
listen: format!("127.0.0.1:{port}").parse()?,
|
|
data_dir: root.join(".state"),
|
|
jellyfin: JellyfinConfig {
|
|
url: jf,
|
|
api_key: "demo".into(),
|
|
},
|
|
path_map: Default::default(),
|
|
peer: Some(PeerConfig {
|
|
url: format!("http://127.0.0.1:{peer_port}"),
|
|
token: "demo-token".into(),
|
|
}),
|
|
incoming_libraries: Default::default(),
|
|
// Throttled so you can watch the progress bar.
|
|
transfer: TransferConfig {
|
|
bandwidth_limit_mbps: 200.0,
|
|
..Default::default()
|
|
},
|
|
ui: UiConfig::default(),
|
|
hooks: HooksConfig::default(),
|
|
};
|
|
std::fs::create_dir_all(&cfg.data_dir)?;
|
|
let store = jm_store::Store::open(&cfg.data_dir.join("db"))?;
|
|
let state = Arc::new(jm_server::state::AppState::new(cfg, store)?);
|
|
jm_server::runner::resume_all(&state).await?;
|
|
let listener = tokio::net::TcpListener::bind(("127.0.0.1", port)).await?;
|
|
tasks.push(tokio::spawn(async move {
|
|
axum::serve(listener, jm_server::api::router(state)).await
|
|
}));
|
|
}
|
|
println!("demo media in {}", root.display());
|
|
println!("cloud: http://127.0.0.1:8097 home: http://127.0.0.1:8098 (Ctrl+C to stop)");
|
|
tokio::signal::ctrl_c().await?;
|
|
Ok(())
|
|
}
|
|
|
|
fn tracing_subscriber_init() {
|
|
// Keep the demo quiet unless asked.
|
|
if std::env::var_os("RUST_LOG").is_some() {
|
|
eprintln!("(set up your own subscriber for logs)");
|
|
}
|
|
}
|