Add a Trash view to see and empty .jellymove-trash (#3)
CI / test (push) Successful in 1m18s

Moved folders sat in each library's .jellymove-trash until removed by hand on the server. The new Trash tab lists them on either server with
size, file count, when and where they went, and empties one item, a library or everything after a confirmation.
- trash.rs: list and empty entries. Entries are referred to by an opaque ID and resolved by scanning; only real folders directly inside a trash folder can be deleted.
- Each move now gets its own trash entry folder, with a small metadata file. Fixes moves failing when the same item had been trashed before.
- Peer API: GET /trash, POST /trash/empty, and /exports/{id}/complete now records the job and destination.
- e2e tests for listing, emptying from either side, refusing paths as IDs, and moving the same item away twice.

Closes #3
This commit is contained in:
IxianPixel committed 2026-11-06 16:30:54 +00:00
1 parent 38319e0987
commit 4c6217bd40
16 files changed
+935 -23

No files matched your search

+46 -10
View File
@@ -15,6 +15,7 @@ use jm_jellyfin::ItemQuery;
use serde::{Deserialize, Serialize};
use crate::state::{AppState, Shared};
use crate::trash;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "detail")]
@@ -272,9 +273,17 @@ fn push_record(
}
}
/// Who finished the move; recorded with the trash entry. Older peers send
/// nothing.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CompleteRequest {
pub job_id: Option<String>,
pub moved_to: Option<String>,
}
/// Final step on the source: move the folder into a hidden trash folder in
/// the same library (same filesystem, so it's a rename) and rescan.
pub async fn complete(state: &Shared, id: &str) -> anyhow::Result<Export> {
pub async fn complete(state: &Shared, id: &str, req: CompleteRequest) -> anyhow::Result<Export> {
let mut export: Export = state
.store
.get_export(id)
@@ -291,21 +300,48 @@ pub async fn complete(state: &Shared, id: &str) -> anyhow::Result<Export> {
.cfg
.path_map
.to_local(Path::new(&export.jellyfin_location));
let trash_root = location_local.join(".jellymove-trash");
let trash = join_relative(&trash_root.join(&export.id), &info.rel_folder)
.context("unsafe folder name")?;
let trash_root = location_local.join(trash::TRASH_DIR);
// Re-running after a crash: if the folder is already gone it was moved
// last time, so only the rescan and status update remain.
if tokio::fs::metadata(&local_folder).await.is_ok() {
if let Some(parent) = trash.parent() {
tokio::fs::create_dir_all(parent).await?;
}
tokio::fs::create_dir_all(&trash_root).await?;
let marker = trash_root.join(".ignore");
if tokio::fs::metadata(&marker).await.is_err() {
tokio::fs::write(&marker, b"").await?;
}
tokio::fs::rename(&local_folder, &trash)
.await
.with_context(|| format!("moving {} to the trash", local_folder.display()))?;
// The same item can be trashed more than once (moved away, back,
// and away again), so each move gets its own entry folder.
let entry_dir = trash::new_entry_dir(&trash_root, &export.id);
let trash = join_relative(&entry_dir, &info.rel_folder).context("unsafe folder name")?;
if let Some(parent) = trash.parent() {
tokio::fs::create_dir_all(parent).await?;
}
trash::write_meta(
&entry_dir,
&trash::EntryMeta {
name: info.name.clone(),
kind: info.kind,
library: info.library_name.clone(),
rel_folder: info.rel_folder.clone(),
moved_at: time::OffsetDateTime::now_utc(),
moved_to: req.moved_to,
job_id: req.job_id,
},
)?;
if let Err(e) = tokio::fs::rename(&local_folder, &trash).await {
// Don't leave an empty entry behind in the trash view. Only the
// metadata file and empty folders are removed, never contents.
let _ = tokio::fs::remove_file(entry_dir.join(trash::META_FILE)).await;
let mut dir = trash.parent();
while let Some(d) = dir {
if !d.starts_with(&entry_dir) || tokio::fs::remove_dir(d).await.is_err() {
break;
}
dir = d.parent();
}
return Err(e).with_context(|| format!("moving {} to the trash", local_folder.display()));
}
tracing::info!(from = %local_folder.display(), to = %trash.display(), "source moved to trash");
}