From 8e8b60fe311a9dd0991ea3c50068128d376be65c Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 15 Jun 2025 18:40:11 +0300 Subject: [PATCH] cleanup irrelevant files on complete torrent download --- Cargo.toml | 2 ++ src/main.rs | 6 +++++- src/storage.rs | 24 ++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75ce413..14de7f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,13 @@ anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } hyper-util = "0.1" librqbit = {version = "9.0.0-beta.0", features = ["disable-upload"]} +regex = "1.11" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1.45", features = ["full"] } tracing-subscriber = "0.3" url = "2.5" +walkdir = "2.5" [patch.crates-io] librqbit = { git = "https://github.com/ikatson/rqbit.git", package = "librqbit" } #librqbit = { version = "9.0.0-beta.0", path = "../../rqbit/crates/librqbit", package = "librqbit" } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 185e31b..fc02699 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,7 +83,7 @@ async fn main() -> Result<()> { list_only: arg.preload_regex.is_none(), // the destination folder to preload files match `only_files_regex` // * e.g. images for audio albums - output_folder: storage.output_folder(&i).ok(), + output_folder: storage.output_folder(&i, true).ok(), only_files_regex: arg.preload_regex.clone(), ..Default::default() }), @@ -124,6 +124,10 @@ async fn main() -> Result<()> { false, ) .await?; + // cleanup irrelevant files (see rqbit#408) + if let Some(ref r) = arg.preload_regex { + storage.purge_preload_regex(&i, r)?; + } // ignore on the next crawl iterations for this session index.insert(mt.info_hash().as_string()); } diff --git a/src/storage.rs b/src/storage.rs index cc2c11d..93e2679 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -37,17 +37,37 @@ impl Storage { Ok(p) } - pub fn output_folder(&self, infohash: &str) -> Result { + pub fn output_folder(&self, infohash: &str, create: bool) -> Result { let mut p = PathBuf::new(); p.push(&self.0); p.push(infohash); if p.is_file() { bail!("File destination is not directory!"); } - fs::create_dir_all(&p)?; + if create { + fs::create_dir_all(&p)?; + } + if !p.is_dir() { + bail!("Destination directory not exists!") + } Ok(p.to_string_lossy().to_string()) } + /// Recursively remove all files match `pattern` under `infohash` location + pub fn purge_preload_regex(&self, infohash: &str, pattern: &str) -> Result<()> { + let r = regex::Regex::new(pattern)?; + for e in walkdir::WalkDir::new(self.output_folder(infohash, false)?) + .into_iter() + .filter_map(Result::ok) + { + let p = e.path(); + if p.is_file() && !r.is_match(p.to_str().unwrap()) { + std::fs::remove_file(p)?; + } + } + Ok(()) + } + pub fn path(&self) -> PathBuf { self.0.clone() }