CI / test (push) Successful in 1m12s
The library lists now show how much space each movie and show takes on disk, counting everything a move copies, with a per-season breakdown for shows, a running total and a "Largest first" sort. - sizes.rs: a single background worker measures one folder at a time. Listing returns cached sizes straight away and queues folders; the UI polls until every size has arrived. - Cached in a new folder_sizes table (store migration v2). A size is kept while the folder's signature (mtimes of the folder and its direct subfolders) is unchanged, checked at most hourly and re-measured after 7 days. - Each agent measures its own folders, so peer listings carry the peer's sizes. - Tests for measuring, signatures, natural season order, the migration, and an e2e test covering both sides and a moved item.
147 lines
4.4 KiB
Rust
147 lines
4.4 KiB
Rust
//! Browsing the local Jellyfin libraries.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use jm_jellyfin::ItemQuery;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::sizes::{self, FolderSize};
|
|
use crate::state::{AppState, Shared};
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Library {
|
|
pub id: String,
|
|
pub name: String,
|
|
pub collection_type: Option<String>,
|
|
pub locations: Vec<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct LibraryItem {
|
|
pub id: String,
|
|
pub name: String,
|
|
/// "Movie" or "Series".
|
|
pub kind: String,
|
|
pub year: Option<i32>,
|
|
pub path: Option<String>,
|
|
/// Size of the item's folder on disk; `None` until it has been measured.
|
|
#[serde(default)]
|
|
pub size: Option<FolderSize>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct LocationCheck {
|
|
pub library: String,
|
|
pub jellyfin_path: String,
|
|
pub local_path: String,
|
|
pub exists: bool,
|
|
pub writable: bool,
|
|
}
|
|
|
|
/// Only libraries jellymove knows how to move items out of.
|
|
fn supported(collection_type: Option<&str>) -> bool {
|
|
matches!(collection_type, Some("movies") | Some("tvshows") | None)
|
|
}
|
|
|
|
pub async fn libraries(state: &AppState) -> anyhow::Result<Vec<Library>> {
|
|
Ok(state
|
|
.jellyfin
|
|
.virtual_folders()
|
|
.await?
|
|
.into_iter()
|
|
.filter(|v| supported(v.collection_type.as_deref()))
|
|
.map(|v| Library {
|
|
id: v.item_id,
|
|
name: v.name,
|
|
collection_type: v.collection_type,
|
|
locations: v.locations,
|
|
})
|
|
.collect())
|
|
}
|
|
|
|
/// The movies and series in a library, with the size of each one's folder if
|
|
/// it's known yet. Sizes still missing are being measured in the background.
|
|
pub async fn items(
|
|
state: &Shared,
|
|
library_id: &str,
|
|
search: Option<&str>,
|
|
) -> anyhow::Result<Vec<LibraryItem>> {
|
|
let search = search.map(str::trim).filter(|s| !s.is_empty());
|
|
let items = state
|
|
.jellyfin
|
|
.items(&ItemQuery {
|
|
parent_id: Some(library_id),
|
|
include_types: &["Movie", "Series"],
|
|
recursive: true,
|
|
search,
|
|
..Default::default()
|
|
})
|
|
.await?;
|
|
let mut out: Vec<LibraryItem> = items
|
|
.into_iter()
|
|
.map(|i| LibraryItem {
|
|
id: i.id,
|
|
name: i.name,
|
|
kind: i.kind,
|
|
year: i.production_year,
|
|
path: i.path,
|
|
size: None,
|
|
})
|
|
.collect();
|
|
|
|
// Only items whose folder we can place are measured.
|
|
let folders: Vec<(usize, PathBuf)> = out
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(n, i)| Some((n, item_folder(state, i)?)))
|
|
.collect();
|
|
let paths: Vec<PathBuf> = folders.iter().map(|(_, p)| p.clone()).collect();
|
|
let sizes = sizes::lookup(state, &paths).await?;
|
|
for ((n, _), size) in folders.into_iter().zip(sizes) {
|
|
out[n].size = size;
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// The folder a move would take, as this agent sees it: a movie's parent
|
|
/// folder, or the series folder itself.
|
|
fn item_folder(state: &AppState, item: &LibraryItem) -> Option<PathBuf> {
|
|
let path = Path::new(item.path.as_deref()?);
|
|
let folder = match item.kind.as_str() {
|
|
"Movie" => path.parent()?,
|
|
"Series" => path,
|
|
_ => return None,
|
|
};
|
|
Some(state.cfg.path_map.to_local(folder))
|
|
}
|
|
|
|
/// Confirm every library location exists and is writable from this agent.
|
|
/// The most common setup mistakes (wrong mount, wrong PUID/PGID, missing
|
|
/// path_map) all show up here.
|
|
pub async fn check_locations(state: &AppState) -> anyhow::Result<Vec<LocationCheck>> {
|
|
let mut out = Vec::new();
|
|
for lib in libraries(state).await? {
|
|
for loc in &lib.locations {
|
|
let local = state.cfg.path_map.to_local(Path::new(loc));
|
|
let exists = tokio::fs::metadata(&local)
|
|
.await
|
|
.map(|m| m.is_dir())
|
|
.unwrap_or(false);
|
|
let writable = exists && {
|
|
let probe = local.join(format!(".jellymove-probe-{}", uuid::Uuid::new_v4()));
|
|
let ok = tokio::fs::write(&probe, b"").await.is_ok();
|
|
let _ = tokio::fs::remove_file(&probe).await;
|
|
ok
|
|
};
|
|
out.push(LocationCheck {
|
|
library: lib.name.clone(),
|
|
jellyfin_path: loc.clone(),
|
|
local_path: local.display().to_string(),
|
|
exists,
|
|
writable,
|
|
});
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|