diff --git a/Cargo.lock b/Cargo.lock index 2212373..e948a02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,6 +134,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "strsim" version = "0.11.1" @@ -156,6 +165,7 @@ name = "transmission-torrent-loader" version = "0.1.0" dependencies = [ "clap", + "walkdir", ] [[package]] @@ -170,6 +180,25 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-sys" version = "0.59.0" diff --git a/Cargo.toml b/Cargo.toml index 37c8c66..661ebef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] clap = { version = "4.5.39", features = ["derive"] } +walkdir = "2.5.0" diff --git a/src/main.rs b/src/main.rs index a611ee4..72590a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use clap::Parser; +use std::path::{Path, PathBuf}; +use walkdir::WalkDir; #[derive(Parser, Debug)] #[command( @@ -8,19 +10,29 @@ use clap::Parser; long_about = None )] struct Args { - /// Source path (original file/directory) + /// Source path (root of directory tree with torrent files) #[arg(short = 's', long = "source", required = true)] source: String, - /// Destination path (target file/directory) + /// Destination path (download root) #[arg(short = 'd', long = "destination", required = true)] destination: String, - /// Rename flag (whether to rename files) + /// Rename flag (whether to rename root directory of torrent, if exists) #[arg(short = 'r', long = "rename", default_value_t = false)] rename: bool, } +fn find_torrent_files(start_path: &Path) -> Vec { + WalkDir::new(start_path) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.file_type().is_file()) + .filter(|e| e.path().extension().unwrap_or_default().to_str() == Some("torrent")) + .map(|e| e.into_path()) + .collect() +} + fn main() { let args = Args::parse(); @@ -28,4 +40,8 @@ fn main() { println!("Source path: {}", args.source); println!("Destination path: {}", args.destination); println!("Rename: {}", args.rename); + let torrent_files = find_torrent_files(Path::new(&args.source)); + for file in torrent_files { + println!("{}", file.display()); + } } diff --git a/tasks.md b/tasks.md index cb121e4..57cb967 100755 --- a/tasks.md +++ b/tasks.md @@ -9,7 +9,6 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc ## Задачи - Создать привязку к systemd: service и timer - https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html -- Обходить дерево директорий - Создавать новую директорию в нужном месте - Вскрывать торрент-файл, чтобы понять нужно ли переименование - Если файл один, то не нужно, но нужно создавать директорию @@ -22,3 +21,4 @@ https://chat.deepseek.com/a/chat/s/70331337-52dd-4f53-9344-c680977f73dc - source - destination - rename +- Обходить дерево директорий