fix recursive directory scanning in daemon and CLI list_local_directory
- SHA
df39b56624a05f5612fb7853576322949a73bf6c- Parents
-
23cc7a7 - Tree
095ce01
df39b56
df39b56624a05f5612fb7853576322949a73bf6c23cc7a7
095ce01| Status | File | + | - |
|---|---|---|---|
| M |
garbg/src/daemon/state.rs
|
13 | 6 |
| M |
garbg/src/main.rs
|
15 | 6 |
garbg/src/daemon/state.rsmodified@@ -1344,7 +1344,7 @@ impl Daemon { | |||
| 1344 | self.list_local_directory(&expanded) | 1344 | self.list_local_directory(&expanded) |
| 1345 | } | 1345 | } |
| 1346 | 1346 | ||
| 1347 | - /// List images in a local directory | 1347 | + /// List images in a local directory (recursively) |
| 1348 | fn list_local_directory(&self, path: &str) -> Result<Vec<String>> { | 1348 | fn list_local_directory(&self, path: &str) -> Result<Vec<String>> { |
| 1349 | let dir_path = std::path::Path::new(path); | 1349 | let dir_path = std::path::Path::new(path); |
| 1350 | 1350 | ||
@@ -1357,16 +1357,23 @@ impl Daemon { | |||
| 1357 | } | 1357 | } |
| 1358 | 1358 | ||
| 1359 | let mut images = Vec::new(); | 1359 | let mut images = Vec::new(); |
| 1360 | - for entry in std::fs::read_dir(dir_path)? { | 1360 | + Self::collect_images_recursive(dir_path, &mut images)?; |
| 1361 | + images.sort(); | ||
| 1362 | + Ok(images) | ||
| 1363 | + } | ||
| 1364 | + | ||
| 1365 | + /// Recursively collect supported image files from a directory. | ||
| 1366 | + fn collect_images_recursive(dir: &std::path::Path, images: &mut Vec<String>) -> Result<()> { | ||
| 1367 | + for entry in std::fs::read_dir(dir)? { | ||
| 1361 | let entry = entry?; | 1368 | let entry = entry?; |
| 1362 | let entry_path = entry.path(); | 1369 | let entry_path = entry.path(); |
| 1363 | - if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) { | 1370 | + if entry_path.is_dir() { |
| 1371 | + Self::collect_images_recursive(&entry_path, images)?; | ||
| 1372 | + } else if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) { | ||
| 1364 | images.push(entry_path.to_string_lossy().to_string()); | 1373 | images.push(entry_path.to_string_lossy().to_string()); |
| 1365 | } | 1374 | } |
| 1366 | } | 1375 | } |
| 1367 | - | 1376 | + Ok(()) |
| 1368 | - images.sort(); | ||
| 1369 | - Ok(images) | ||
| 1370 | } | 1377 | } |
| 1371 | 1378 | ||
| 1372 | /// Get connected monitors info via RandR | 1379 | /// Get connected monitors info via RandR |
garbg/src/main.rsmodified@@ -641,7 +641,7 @@ fn normalize_github_url(source: &str) -> String { | |||
| 641 | source.to_string() | 641 | source.to_string() |
| 642 | } | 642 | } |
| 643 | 643 | ||
| 644 | -/// List images in a local directory | 644 | +/// List images in a local directory (recursively) |
| 645 | fn list_local_directory(path: &str) -> Result<Vec<String>> { | 645 | fn list_local_directory(path: &str) -> Result<Vec<String>> { |
| 646 | use garbg::media::ImageLoader; | 646 | use garbg::media::ImageLoader; |
| 647 | use std::path::Path; | 647 | use std::path::Path; |
@@ -659,16 +659,25 @@ fn list_local_directory(path: &str) -> Result<Vec<String>> { | |||
| 659 | } | 659 | } |
| 660 | 660 | ||
| 661 | let mut images = Vec::new(); | 661 | let mut images = Vec::new(); |
| 662 | - for entry in std::fs::read_dir(dir_path)? { | 662 | + collect_images_recursive(dir_path, &mut images)?; |
| 663 | + images.sort(); | ||
| 664 | + Ok(images) | ||
| 665 | +} | ||
| 666 | + | ||
| 667 | +/// Recursively collect supported image files from a directory. | ||
| 668 | +fn collect_images_recursive(dir: &std::path::Path, images: &mut Vec<String>) -> Result<()> { | ||
| 669 | + use garbg::media::ImageLoader; | ||
| 670 | + | ||
| 671 | + for entry in std::fs::read_dir(dir)? { | ||
| 663 | let entry = entry?; | 672 | let entry = entry?; |
| 664 | let entry_path = entry.path(); | 673 | let entry_path = entry.path(); |
| 665 | - if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) { | 674 | + if entry_path.is_dir() { |
| 675 | + collect_images_recursive(&entry_path, images)?; | ||
| 676 | + } else if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) { | ||
| 666 | images.push(entry_path.to_string_lossy().to_string()); | 677 | images.push(entry_path.to_string_lossy().to_string()); |
| 667 | } | 678 | } |
| 668 | } | 679 | } |
| 669 | - | 680 | + Ok(()) |
| 670 | - images.sort(); | ||
| 671 | - Ok(images) | ||
| 672 | } | 681 | } |
| 673 | 682 | ||
| 674 | /// List images from a GitHub directory | 683 | /// List images from a GitHub directory |