All checks were successful
Validate Kaleido Desktop / validate (push) Successful in 1m22s
- Clear cached library data when the account or profile behind the server connection changes, not only on Home user switches. - TitleArt falls back to the title when there is no server, so the player no longer crashes offline. - Playback keeps the server it started on: progress, watched state and transcodes never go to a server selected later, a download only reports to its own server, and switching account or server ends a stream from the old one. - Closing or replacing playback during setup can no longer start the file afterwards. - Transcodes save the chosen audio/subtitle tracks on the server before starting (and name them in the request), apply the language and subtitle preferences, and keep the current tracks across quality changes. - Offline playback saves its resume point and watched count in the download's metadata. - Download tasks take over from a previous task for the same title only after it exits, and pause/remove stay wired to the current task. - Downloads prefer a single-file version and refuse split ones instead of saving only part one. - Download artwork is named per file, so episodes in one season folder no longer overwrite or delete each other's images. - Library grids refetch loaded pages when the cache is invalidated. - Request timeouts and cancellation also cover the response body. - Hero rotation resumes after being scrolled back into view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
507 lines
18 KiB
Rust
507 lines
18 KiB
Rust
//! Offline downloads: resumable HTTP transfers of Plex media parts with a small
|
|
//! JSON catalog, so downloaded titles stay browsable and playable offline.
|
|
//!
|
|
//! URLs carrying Plex tokens are only held in memory. The catalog stores the
|
|
//! server-relative source path; resuming after a restart asks the interface for
|
|
//! a freshly signed URL.
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
|
|
|
use futures_util::StreamExt;
|
|
use parking_lot::Mutex;
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::Value;
|
|
use tauri::{AppHandle, Emitter, Manager, Runtime};
|
|
use tokio::io::AsyncWriteExt;
|
|
use tokio::sync::{watch, Semaphore};
|
|
|
|
const MAX_ACTIVE: usize = 2;
|
|
|
|
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum Status {
|
|
Queued,
|
|
Downloading,
|
|
Paused,
|
|
Completed,
|
|
Failed,
|
|
}
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Item {
|
|
pub id: String,
|
|
pub file_path: String,
|
|
pub total: u64,
|
|
pub received: u64,
|
|
pub status: Status,
|
|
#[serde(default)]
|
|
pub error: Option<String>,
|
|
#[serde(default)]
|
|
pub meta: Value,
|
|
pub created_at: u64,
|
|
#[serde(default)]
|
|
pub speed: u64,
|
|
}
|
|
|
|
/// The transfer task that currently owns a download (and its `.part` file).
|
|
struct Worker {
|
|
generation: u64,
|
|
cancel: watch::Sender<bool>,
|
|
/// Closes once the task has exited and let go of the file.
|
|
done: watch::Receiver<()>,
|
|
}
|
|
|
|
pub struct Downloads {
|
|
items: Mutex<Vec<Item>>,
|
|
dir: Mutex<Option<PathBuf>>,
|
|
workers: Mutex<HashMap<String, Worker>>,
|
|
generation: AtomicU64,
|
|
permits: Arc<Semaphore>,
|
|
client: reqwest::Client,
|
|
}
|
|
|
|
impl Default for Downloads {
|
|
fn default() -> Self {
|
|
Downloads {
|
|
items: Mutex::new(Vec::new()),
|
|
dir: Mutex::new(None),
|
|
workers: Mutex::new(HashMap::new()),
|
|
generation: AtomicU64::new(0),
|
|
permits: Arc::new(Semaphore::new(MAX_ACTIVE)),
|
|
client: reqwest::Client::builder()
|
|
.connect_timeout(Duration::from_secs(20))
|
|
.user_agent("Kaleido")
|
|
.build()
|
|
.expect("http client"),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn now() -> u64 {
|
|
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
|
|
}
|
|
|
|
fn catalog_path<R: Runtime>(app: &AppHandle<R>) -> PathBuf {
|
|
crate::system::data_dir(app).join("downloads.json")
|
|
}
|
|
|
|
fn save<R: Runtime>(app: &AppHandle<R>) {
|
|
let state = app.state::<Downloads>();
|
|
let items = state.items.lock().clone();
|
|
let path = catalog_path(app);
|
|
if let Some(parent) = path.parent() {
|
|
let _ = std::fs::create_dir_all(parent);
|
|
}
|
|
if let Ok(text) = serde_json::to_string_pretty(&items) {
|
|
let tmp = path.with_extension("json.tmp");
|
|
if std::fs::write(&tmp, text).is_ok() {
|
|
let _ = std::fs::rename(tmp, path);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Loads the catalog at startup. Interrupted transfers become paused: their
|
|
/// signed URLs were never persisted.
|
|
pub fn restore<R: Runtime>(app: &AppHandle<R>) {
|
|
let Ok(text) = std::fs::read_to_string(catalog_path(app)) else { return };
|
|
let Ok(mut items) = serde_json::from_str::<Vec<Item>>(&text) else { return };
|
|
for item in items.iter_mut() {
|
|
if matches!(item.status, Status::Downloading | Status::Queued) {
|
|
// Marked so the interface resumes it once it has a signed URL again.
|
|
item.status = Status::Paused;
|
|
item.error = Some("interrupted".into());
|
|
}
|
|
if item.status == Status::Completed && !Path::new(&item.file_path).exists() {
|
|
item.status = Status::Failed;
|
|
item.error = Some("The downloaded file is missing.".into());
|
|
}
|
|
item.speed = 0;
|
|
}
|
|
*app.state::<Downloads>().items.lock() = items;
|
|
}
|
|
|
|
fn update<R: Runtime>(app: &AppHandle<R>, id: &str, f: impl FnOnce(&mut Item)) -> Option<Item> {
|
|
let state = app.state::<Downloads>();
|
|
let mut items = state.items.lock();
|
|
let item = items.iter_mut().find(|i| i.id == id)?;
|
|
f(item);
|
|
let snapshot = item.clone();
|
|
drop(items);
|
|
let _ = app.emit("download", &snapshot);
|
|
Some(snapshot)
|
|
}
|
|
|
|
fn sanitize(name: &str) -> String {
|
|
let cleaned: String = name
|
|
.chars()
|
|
.map(|c| if "<>:\"/\\|?*".contains(c) || c.is_control() { '_' } else { c })
|
|
.collect();
|
|
let trimmed = cleaned.trim().trim_end_matches('.').to_string();
|
|
if trimmed.is_empty() {
|
|
"Untitled".into()
|
|
} else {
|
|
trimmed.chars().take(120).collect()
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct StartRequest {
|
|
id: String,
|
|
url: String,
|
|
folder: String,
|
|
file_name: String,
|
|
#[serde(default)]
|
|
meta: Value,
|
|
/// Artwork to keep with the download: name → signed URL.
|
|
#[serde(default)]
|
|
assets: HashMap<String, String>,
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn download_start<R: Runtime>(app: AppHandle<R>, request: StartRequest) -> Result<Item, String> {
|
|
let state = app.state::<Downloads>();
|
|
let dir = state.dir.lock().clone().ok_or("Choose a download folder first.")?;
|
|
// The folder may be nested ("Show/Season 1"); sanitize each component.
|
|
let folder = request
|
|
.folder
|
|
.split(['/', '\\'])
|
|
.filter(|c| !c.trim().is_empty() && *c != "." && *c != "..")
|
|
.fold(dir.clone(), |p, c| p.join(sanitize(c)));
|
|
tokio::fs::create_dir_all(&folder).await.map_err(|e| format!("Cannot create the download folder: {e}"))?;
|
|
let file_path = folder.join(sanitize(&request.file_name));
|
|
|
|
let mut meta = request.meta.clone();
|
|
if let Value::Object(map) = &mut meta {
|
|
let mut local = serde_json::Map::new();
|
|
// Episodes share their season's folder, so artwork is named after the file it
|
|
// belongs to: one episode's still never replaces another's.
|
|
let stem = file_path.file_stem().and_then(|s| s.to_str()).map(sanitize).unwrap_or_else(|| sanitize(&request.id));
|
|
for (name, url) in &request.assets {
|
|
let target = folder.join(format!(".{stem}.{}.jpg", sanitize(name)));
|
|
if fetch_to(&state.client, url, &target).await.is_ok() {
|
|
local.insert(name.clone(), Value::String(target.display().to_string()));
|
|
}
|
|
}
|
|
map.insert("localAssets".into(), Value::Object(local));
|
|
}
|
|
|
|
let item = Item {
|
|
id: request.id.clone(),
|
|
file_path: file_path.display().to_string(),
|
|
total: 0,
|
|
received: 0,
|
|
status: Status::Queued,
|
|
error: None,
|
|
meta,
|
|
created_at: now(),
|
|
speed: 0,
|
|
};
|
|
{
|
|
let mut items = state.items.lock();
|
|
items.retain(|i| i.id != item.id);
|
|
items.insert(0, item.clone());
|
|
}
|
|
save(&app);
|
|
let _ = app.emit("download", &item);
|
|
spawn(app.clone(), request.id, request.url);
|
|
Ok(item)
|
|
}
|
|
|
|
async fn fetch_to(client: &reqwest::Client, url: &str, target: &Path) -> Result<(), String> {
|
|
let bytes = client.get(url).send().await.map_err(|e| e.to_string())?.error_for_status().map_err(|e| e.to_string())?.bytes().await.map_err(|e| e.to_string())?;
|
|
tokio::fs::write(target, bytes).await.map_err(|e| e.to_string())
|
|
}
|
|
|
|
/// Starts a transfer task for `id`, taking over from any task still running for it:
|
|
/// that one is cancelled and the new one waits for it to exit before touching the file.
|
|
fn spawn<R: Runtime>(app: AppHandle<R>, id: String, url: String) {
|
|
let state = app.state::<Downloads>();
|
|
let generation = state.generation.fetch_add(1, Ordering::Relaxed);
|
|
let (tx, rx) = watch::channel(false);
|
|
let (done_tx, done_rx) = watch::channel(());
|
|
let previous = state.workers.lock().insert(id.clone(), Worker { generation, cancel: tx, done: done_rx });
|
|
let previous = previous.map(|w| {
|
|
let _ = w.cancel.send(true);
|
|
w.done
|
|
});
|
|
tauri::async_runtime::spawn(async move {
|
|
// Held until this task ends: whoever waits on `done` then knows the file is free.
|
|
let _done = done_tx;
|
|
let took_over = previous.is_some();
|
|
if let Some(done) = previous {
|
|
exited(done).await;
|
|
}
|
|
let stopped = rx.clone();
|
|
if *stopped.borrow() {
|
|
release(&app, &id, generation);
|
|
return;
|
|
}
|
|
let permits = app.state::<Downloads>().permits.clone();
|
|
let _permit = tokio::select! {
|
|
p = permits.acquire_owned() => p,
|
|
_ = cancelled(rx.clone()) => { release(&app, &id, generation); return; }
|
|
};
|
|
// The task it replaced may have finished the file just as it was stopped.
|
|
let result = if took_over && finished_file(&app, &id) { Ok(true) } else { transfer(&app, &id, &url, rx).await };
|
|
release(&app, &id, generation);
|
|
// Once paused, removed or replaced, the outcome is no longer this task's to
|
|
// report, except a finished file, which is complete whatever happened since.
|
|
if *stopped.borrow() && !matches!(result, Ok(true)) {
|
|
return;
|
|
}
|
|
match result {
|
|
Ok(true) => {
|
|
update(&app, &id, |i| {
|
|
i.status = Status::Completed;
|
|
i.error = None;
|
|
i.speed = 0;
|
|
i.received = i.total.max(i.received);
|
|
});
|
|
}
|
|
Ok(false) => {}
|
|
Err(e) => {
|
|
update(&app, &id, |i| {
|
|
i.status = Status::Failed;
|
|
i.error = Some(e);
|
|
i.speed = 0;
|
|
});
|
|
}
|
|
}
|
|
save(&app);
|
|
});
|
|
}
|
|
|
|
/// Drops the worker entry, unless a newer task has taken it over.
|
|
fn release<R: Runtime>(app: &AppHandle<R>, id: &str, generation: u64) {
|
|
let state = app.state::<Downloads>();
|
|
let mut workers = state.workers.lock();
|
|
if workers.get(id).map(|w| w.generation) == Some(generation) {
|
|
workers.remove(id);
|
|
}
|
|
}
|
|
|
|
/// The download's file is in place and nothing is left to transfer.
|
|
fn finished_file<R: Runtime>(app: &AppHandle<R>, id: &str) -> bool {
|
|
let Some(path) = app.state::<Downloads>().items.lock().iter().find(|i| i.id == id).map(|i| PathBuf::from(&i.file_path)) else { return false };
|
|
path.exists() && !part_path(&path).exists()
|
|
}
|
|
|
|
fn part_path(path: &Path) -> PathBuf {
|
|
path.with_extension(format!("{}.part", path.extension().and_then(|e| e.to_str()).unwrap_or("bin")))
|
|
}
|
|
|
|
/// Resolves once a worker task has exited.
|
|
async fn exited(mut done: watch::Receiver<()>) {
|
|
while done.changed().await.is_ok() {}
|
|
}
|
|
|
|
async fn cancelled(mut rx: watch::Receiver<bool>) {
|
|
while !*rx.borrow() {
|
|
if rx.changed().await.is_err() {
|
|
std::future::pending::<()>().await;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Returns Ok(true) when complete, Ok(false) when paused/removed.
|
|
async fn transfer<R: Runtime>(app: &AppHandle<R>, id: &str, url: &str, rx: watch::Receiver<bool>) -> Result<bool, String> {
|
|
let Some(item) = app.state::<Downloads>().items.lock().iter().find(|i| i.id == id).cloned() else { return Ok(false) };
|
|
let final_path = PathBuf::from(&item.file_path);
|
|
let part_path = part_path(&final_path);
|
|
let existing = tokio::fs::metadata(&part_path).await.map(|m| m.len()).unwrap_or(0);
|
|
|
|
let client = app.state::<Downloads>().client.clone();
|
|
let mut request = client.get(url);
|
|
if existing > 0 {
|
|
request = request.header("Range", format!("bytes={existing}-"));
|
|
}
|
|
let response = tokio::select! {
|
|
r = request.send() => r.map_err(|e| format!("Could not reach the server: {e}"))?,
|
|
_ = cancelled(rx.clone()) => return Ok(false),
|
|
};
|
|
let status = response.status();
|
|
if status.as_u16() == 401 || status.as_u16() == 403 {
|
|
return Err("The server does not allow downloads for this account.".into());
|
|
}
|
|
if !status.is_success() {
|
|
return Err(format!("The server could not provide this file ({status})."));
|
|
}
|
|
let resumed = status.as_u16() == 206;
|
|
let start = if resumed { existing } else { 0 };
|
|
let total = response.content_length().map(|l| l + start).unwrap_or(0);
|
|
let mut file = tokio::fs::OpenOptions::new()
|
|
.create(true)
|
|
.write(true)
|
|
.append(resumed)
|
|
.truncate(!resumed)
|
|
.open(&part_path)
|
|
.await
|
|
.map_err(|e| format!("Cannot write the download: {e}"))?;
|
|
|
|
update(app, id, |i| {
|
|
i.status = Status::Downloading;
|
|
i.total = total;
|
|
i.received = start;
|
|
i.error = None;
|
|
});
|
|
save(app);
|
|
|
|
let mut stream = response.bytes_stream();
|
|
let mut received = start;
|
|
let mut last_emit = Instant::now();
|
|
let mut last_save = Instant::now();
|
|
let mut window_bytes = 0u64;
|
|
let mut window_start = Instant::now();
|
|
let mut speed = 0u64;
|
|
loop {
|
|
if *rx.borrow() {
|
|
file.flush().await.ok();
|
|
return Ok(false);
|
|
}
|
|
let chunk = tokio::select! {
|
|
c = stream.next() => c,
|
|
_ = cancelled(rx.clone()) => { file.flush().await.ok(); return Ok(false); }
|
|
};
|
|
let Some(chunk) = chunk else { break };
|
|
let chunk = chunk.map_err(|e| format!("The transfer was interrupted: {e}"))?;
|
|
file.write_all(&chunk).await.map_err(|e| format!("Cannot write the download: {e}"))?;
|
|
received += chunk.len() as u64;
|
|
window_bytes += chunk.len() as u64;
|
|
let elapsed = window_start.elapsed();
|
|
if elapsed >= Duration::from_secs(1) {
|
|
speed = (window_bytes as f64 / elapsed.as_secs_f64()) as u64;
|
|
window_bytes = 0;
|
|
window_start = Instant::now();
|
|
}
|
|
if last_emit.elapsed() >= Duration::from_millis(250) {
|
|
last_emit = Instant::now();
|
|
update(app, id, |i| {
|
|
i.received = received;
|
|
i.speed = speed;
|
|
});
|
|
}
|
|
if last_save.elapsed() >= Duration::from_secs(5) {
|
|
last_save = Instant::now();
|
|
save(app);
|
|
}
|
|
}
|
|
file.flush().await.map_err(|e| e.to_string())?;
|
|
drop(file);
|
|
if total > 0 && received < total {
|
|
return Err("The transfer ended early. Resume to continue.".into());
|
|
}
|
|
tokio::fs::rename(&part_path, &final_path).await.map_err(|e| format!("Cannot finish the download: {e}"))?;
|
|
update(app, id, |i| i.received = received);
|
|
Ok(true)
|
|
}
|
|
|
|
/// Stops the transfer for `id`, if one is running; the receiver closes once it has exited.
|
|
/// The entry stays until then, so a task started meanwhile knows to wait for it.
|
|
fn cancel<R: Runtime>(app: &AppHandle<R>, id: &str) -> Option<watch::Receiver<()>> {
|
|
let state = app.state::<Downloads>();
|
|
let workers = state.workers.lock();
|
|
let worker = workers.get(id)?;
|
|
let _ = worker.cancel.send(true);
|
|
Some(worker.done.clone())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn download_pause<R: Runtime>(app: AppHandle<R>, id: String) {
|
|
cancel(&app, &id);
|
|
update(&app, &id, |i| {
|
|
if i.status != Status::Completed {
|
|
i.status = Status::Paused;
|
|
i.speed = 0;
|
|
}
|
|
});
|
|
save(&app);
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn download_resume<R: Runtime>(app: AppHandle<R>, id: String, url: String) {
|
|
// `spawn` takes over from a transfer still winding down.
|
|
update(&app, &id, |i| {
|
|
i.status = Status::Queued;
|
|
i.error = None;
|
|
});
|
|
spawn(app, id, url);
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn download_remove<R: Runtime>(app: AppHandle<R>, id: String, delete_files: bool) -> Result<(), String> {
|
|
let running = cancel(&app, &id);
|
|
let removed = {
|
|
let state = app.state::<Downloads>();
|
|
let mut items = state.items.lock();
|
|
let pos = items.iter().position(|i| i.id == id);
|
|
pos.map(|p| items.remove(p))
|
|
};
|
|
if let (Some(item), true) = (removed, delete_files) {
|
|
let path = PathBuf::from(&item.file_path);
|
|
// Let the cancelled transfer release its file handle first.
|
|
if let Some(done) = running {
|
|
let _ = tokio::time::timeout(Duration::from_secs(10), exited(done)).await;
|
|
}
|
|
let _ = tokio::fs::remove_file(&path).await;
|
|
let _ = tokio::fs::remove_file(part_path(&path)).await;
|
|
// Older downloads shared artwork between a season's episodes: keep what others still use.
|
|
let in_use: HashSet<String> = app
|
|
.state::<Downloads>()
|
|
.items
|
|
.lock()
|
|
.iter()
|
|
.filter_map(|i| i.meta.get("localAssets").and_then(Value::as_object))
|
|
.flat_map(|a| a.values().filter_map(Value::as_str).map(str::to_owned))
|
|
.collect();
|
|
if let Value::Object(assets) = &item.meta["localAssets"] {
|
|
for v in assets.values() {
|
|
if let Some(p) = v.as_str().filter(|p| !in_use.contains(*p)) {
|
|
let _ = tokio::fs::remove_file(p).await;
|
|
}
|
|
}
|
|
}
|
|
// Remove now-empty folders (season, then show), never the download root.
|
|
let root = app.state::<Downloads>().dir.lock().clone();
|
|
let mut folder = path.parent().map(|p| p.to_path_buf());
|
|
while let Some(f) = folder {
|
|
if Some(&f) == root.as_ref() || tokio::fs::remove_dir(&f).await.is_err() {
|
|
break;
|
|
}
|
|
folder = f.parent().map(|p| p.to_path_buf());
|
|
}
|
|
}
|
|
save(&app);
|
|
let _ = app.emit("download-removed", &id);
|
|
Ok(())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn download_list<R: Runtime>(app: AppHandle<R>) -> Vec<Item> {
|
|
app.state::<Downloads>().items.lock().clone()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn download_set_dir<R: Runtime>(app: AppHandle<R>, dir: String) -> Result<(), String> {
|
|
let path = PathBuf::from(dir);
|
|
std::fs::create_dir_all(&path).map_err(|e| format!("Cannot use this folder: {e}"))?;
|
|
*app.state::<Downloads>().dir.lock() = Some(path);
|
|
Ok(())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn download_write_meta<R: Runtime>(app: AppHandle<R>, id: String, meta: Value) {
|
|
update(&app, &id, |i| {
|
|
let assets = i.meta.get("localAssets").cloned();
|
|
i.meta = meta;
|
|
if let (Some(a), Value::Object(m)) = (assets, &mut i.meta) {
|
|
m.insert("localAssets".into(), a);
|
|
}
|
|
});
|
|
save(&app);
|
|
}
|