gardesk/garbg / 23cc7a7

Browse files

fix: recurse into subdirectories when scanning for images

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
23cc7a7f3ff8254b2e019514f93e38328d6bcbbf
Parents
c1fca2f
Tree
732d512

1 changed file

StatusFile+-
M garbg/src/sources/file.rs 33 25
garbg/src/sources/file.rsmodified
@@ -15,6 +15,37 @@ impl FileProvider {
1515
         Self
1616
     }
1717
 
18
+    /// Recursively collect supported image files from a directory.
19
+    fn collect_images(dir: &Path, entries: &mut Vec<WallpaperEntry>) -> Result<()> {
20
+        let read_dir = std::fs::read_dir(dir)
21
+            .with_context(|| format!("Failed to read directory: {}", dir.display()))?;
22
+
23
+        for entry in read_dir.flatten() {
24
+            let entry_path = entry.path();
25
+            if entry_path.is_dir() {
26
+                Self::collect_images(&entry_path, entries)?;
27
+            } else if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) {
28
+                let name = entry_path
29
+                    .file_name()
30
+                    .and_then(|n| n.to_str())
31
+                    .unwrap_or("unknown")
32
+                    .to_string();
33
+
34
+                let size = entry.metadata().ok().map(|m| m.len());
35
+
36
+                entries.push(WallpaperEntry {
37
+                    uri: entry_path.to_string_lossy().to_string(),
38
+                    name,
39
+                    media_type: Self::media_type_from_path(&entry_path),
40
+                    size,
41
+                    metadata: Default::default(),
42
+                });
43
+            }
44
+        }
45
+
46
+        Ok(())
47
+    }
48
+
1849
     /// Determine media type from file extension
1950
     fn media_type_from_path(path: &Path) -> MediaType {
2051
         let ext = path
@@ -64,32 +95,9 @@ impl SourceProvider for FileProvider {
6495
                 metadata: Default::default(),
6596
             }])
6697
         } else if path.is_dir() {
67
-            // Directory - list all supported files
98
+            // Directory - recursively list all supported files
6899
             let mut entries = Vec::new();
69
-
70
-            let read_dir = std::fs::read_dir(path)
71
-                .with_context(|| format!("Failed to read directory: {}", path.display()))?;
72
-
73
-            for entry in read_dir.flatten() {
74
-                let entry_path = entry.path();
75
-                if entry_path.is_file() && ImageLoader::is_supported_format(&entry_path) {
76
-                    let name = entry_path
77
-                        .file_name()
78
-                        .and_then(|n| n.to_str())
79
-                        .unwrap_or("unknown")
80
-                        .to_string();
81
-
82
-                    let size = entry.metadata().ok().map(|m| m.len());
83
-
84
-                    entries.push(WallpaperEntry {
85
-                        uri: entry_path.to_string_lossy().to_string(),
86
-                        name,
87
-                        media_type: Self::media_type_from_path(&entry_path),
88
-                        size,
89
-                        metadata: Default::default(),
90
-                    });
91
-                }
92
-            }
100
+            Self::collect_images(path, &mut entries)?;
93101
 
94102
             // Sort by name
95103
             entries.sort_by(|a, b| a.name.cmp(&b.name));