gardesk/garbg / df39b56

Browse files

fix recursive directory scanning in daemon and CLI list_local_directory

Authored by espadonne
SHA
df39b56624a05f5612fb7853576322949a73bf6c
Parents
23cc7a7
Tree
095ce01

2 changed files

StatusFile+-
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 {
13441344
         self.list_local_directory(&expanded)
13451345
     }
13461346
 
1347
-    /// List images in a local directory
1347
+    /// List images in a local directory (recursively)
13481348
     fn list_local_directory(&self, path: &str) -> Result<Vec<String>> {
13491349
         let dir_path = std::path::Path::new(path);
13501350
 
@@ -1357,16 +1357,23 @@ impl Daemon {
13571357
         }
13581358
 
13591359
         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)? {
13611368
             let entry = entry?;
13621369
             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) {
13641373
                 images.push(entry_path.to_string_lossy().to_string());
13651374
             }
13661375
         }
1367
-
1368
-        images.sort();
1369
-        Ok(images)
1376
+        Ok(())
13701377
     }
13711378
 
13721379
     /// Get connected monitors info via RandR
garbg/src/main.rsmodified
@@ -641,7 +641,7 @@ fn normalize_github_url(source: &str) -> String {
641641
     source.to_string()
642642
 }
643643
 
644
-/// List images in a local directory
644
+/// List images in a local directory (recursively)
645645
 fn list_local_directory(path: &str) -> Result<Vec<String>> {
646646
     use garbg::media::ImageLoader;
647647
     use std::path::Path;
@@ -659,16 +659,25 @@ fn list_local_directory(path: &str) -> Result<Vec<String>> {
659659
     }
660660
 
661661
     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)? {
663672
         let entry = entry?;
664673
         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) {
666677
             images.push(entry_path.to_string_lossy().to_string());
667678
         }
668679
     }
669
-
670
-    images.sort();
671
-    Ok(images)
680
+    Ok(())
672681
 }
673682
 
674683
 /// List images from a GitHub directory