Add a Trash view to see and empty .jellymove-trash (#3)
CI / test (push) Successful in 1m18s
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:
1 parent
38319e0987
commit
4c6217bd40
16 files changed
+935
-23
No files matched your search
@@ -375,6 +375,148 @@ async fn refuses_when_destination_exists() {
|
||||
assert!(cloud.movies().join("Alien (1979)").exists());
|
||||
}
|
||||
|
||||
/// Total size of every file below `dir`.
|
||||
fn tree_size(dir: &Path) -> u64 {
|
||||
std::fs::read_dir(dir)
|
||||
.unwrap()
|
||||
.map(|e| {
|
||||
let e = e.unwrap();
|
||||
if e.file_type().unwrap().is_dir() {
|
||||
tree_size(&e.path())
|
||||
} else {
|
||||
e.metadata().unwrap().len()
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
async fn move_item(base: &str, from: &str, item_id: &str, name: &str) -> Value {
|
||||
let resp = post(
|
||||
base,
|
||||
"/api/moves",
|
||||
json!({"from": from, "item_id": item_id, "name": name}),
|
||||
)
|
||||
.await;
|
||||
assert!(resp.status().is_success(), "{}", resp.text().await.unwrap());
|
||||
let job: Value = resp.json().await.unwrap();
|
||||
let done = wait_job(base, job["id"].as_str().unwrap()).await;
|
||||
assert_eq!(done["status"]["kind"], "Done", "{done:#}");
|
||||
done
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn trash_can_be_listed_and_emptied_from_either_side() {
|
||||
let (cloud, home) = pair(seed_movie).await;
|
||||
let size = tree_size(&cloud.movies().join("Alien (1979)"));
|
||||
let movie = cloud.fake.find("Movie", "Alien").unwrap();
|
||||
let job = move_item(&cloud.url, "local", &movie, "Alien").await;
|
||||
|
||||
// The source lists its own trash, with what was moved, where and how big.
|
||||
let local = get(&cloud.url, "/api/trash?node=local").await;
|
||||
let entries = local.as_array().unwrap();
|
||||
assert_eq!(entries.len(), 1, "{local:#}");
|
||||
let e = &entries[0];
|
||||
assert_eq!(e["name"], "Alien");
|
||||
assert_eq!(e["kind"], "Movie");
|
||||
assert_eq!(e["library"], "Movies");
|
||||
assert_eq!(e["folder"], "Alien (1979)");
|
||||
assert_eq!(e["moved_to"], "home");
|
||||
assert_eq!(e["job_id"], job["id"]);
|
||||
assert_eq!(e["size"], size);
|
||||
assert_eq!(e["files"], 4);
|
||||
assert!(e["moved_at"].is_string());
|
||||
|
||||
// The destination sees the same entry as the peer's trash, and its own is empty.
|
||||
let peer = get(&home.url, "/api/trash?node=peer").await;
|
||||
assert_eq!(peer, local);
|
||||
assert_eq!(get(&home.url, "/api/trash?node=local").await, json!([]));
|
||||
|
||||
// Paths are never accepted as IDs.
|
||||
let bogus = post(
|
||||
&home.url,
|
||||
"/api/trash/empty",
|
||||
json!({"node": "peer", "ids": ["../Alien (1979)", "/etc"]}),
|
||||
)
|
||||
.await;
|
||||
let bogus: Value = bogus.json().await.unwrap();
|
||||
assert_eq!(bogus["removed"], 0);
|
||||
assert_eq!(bogus["missing"].as_array().unwrap().len(), 2);
|
||||
assert!(home.movies().join("Alien (1979)/Alien (1979).mkv").exists());
|
||||
|
||||
// Empty it from the destination's UI: the source deletes its own files.
|
||||
let id = e["id"].clone();
|
||||
let resp = post(
|
||||
&home.url,
|
||||
"/api/trash/empty",
|
||||
json!({"node": "peer", "ids": [id]}),
|
||||
)
|
||||
.await;
|
||||
assert!(resp.status().is_success(), "{}", resp.text().await.unwrap());
|
||||
let result: Value = resp.json().await.unwrap();
|
||||
assert_eq!(result["removed"], 1);
|
||||
assert_eq!(result["freed"], size);
|
||||
let trash = cloud.movies().join(".jellymove-trash");
|
||||
let left: Vec<_> = std::fs::read_dir(&trash)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().file_name())
|
||||
.collect();
|
||||
assert_eq!(left, [".ignore"]);
|
||||
assert_eq!(get(&cloud.url, "/api/trash?node=local").await, json!([]));
|
||||
// The moved copy is untouched.
|
||||
assert!(home.movies().join("Alien (1979)/Alien (1979).mkv").exists());
|
||||
|
||||
// Emptying again is harmless.
|
||||
let again: Value = post(
|
||||
&cloud.url,
|
||||
"/api/trash/empty",
|
||||
json!({"node": "local", "ids": [id]}),
|
||||
)
|
||||
.await
|
||||
.json()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(again["removed"], 0);
|
||||
assert_eq!(again["missing"], json!([id]));
|
||||
|
||||
// Like every other UI write, emptying needs the CSRF header.
|
||||
let r = reqwest::Client::new()
|
||||
.post(format!("{}/api/trash/empty", cloud.url))
|
||||
.json(&json!({"ids": []}))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status(), 403);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_item_moved_away_twice_gets_two_trash_entries() {
|
||||
let (cloud, home) = pair(seed_movie).await;
|
||||
// Jellyfin item IDs come from the path, so when the movie comes back to
|
||||
// the same folder it gets the same ID, and so the same export ID.
|
||||
let movie = cloud.fake.find("Movie", "Alien").unwrap();
|
||||
move_item(&cloud.url, "local", &movie, "Alien").await;
|
||||
let on_home = home.fake.find("Movie", "Alien").unwrap();
|
||||
move_item(&cloud.url, "peer", &on_home, "Alien").await;
|
||||
let back = cloud.fake.find("Movie", "Alien").unwrap();
|
||||
assert_eq!(back, movie);
|
||||
move_item(&cloud.url, "local", &back, "Alien").await;
|
||||
|
||||
assert!(home.movies().join("Alien (1979)/Alien (1979).mkv").exists());
|
||||
let cloud_trash = get(&cloud.url, "/api/trash?node=local").await;
|
||||
let entries = cloud_trash.as_array().unwrap();
|
||||
assert_eq!(entries.len(), 2, "{cloud_trash:#}");
|
||||
assert_ne!(entries[0]["id"], entries[1]["id"]);
|
||||
assert!(
|
||||
cloud
|
||||
.movies()
|
||||
.join(format!(".jellymove-trash/item-{movie}-2/Alien (1979)/Alien (1979).mkv"))
|
||||
.exists()
|
||||
);
|
||||
let home_trash = get(&home.url, "/api/trash?node=local").await;
|
||||
assert_eq!(home_trash.as_array().unwrap().len(), 1);
|
||||
assert_eq!(home_trash[0]["moved_to"], "cloud");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn peer_api_rejects_bad_token_and_ui_requires_csrf_header() {
|
||||
let (cloud, _home) = pair(|_| {}).await;
|
||||
|
||||
Reference in new issue
Block a user