@@ -15,6 +15,37 @@ impl FileProvider { |
| 15 | 15 | Self |
| 16 | 16 | } |
| 17 | 17 | |
| 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 | + |
| 18 | 49 | /// Determine media type from file extension |
| 19 | 50 | fn media_type_from_path(path: &Path) -> MediaType { |
| 20 | 51 | let ext = path |
@@ -64,32 +95,9 @@ impl SourceProvider for FileProvider { |
| 64 | 95 | metadata: Default::default(), |
| 65 | 96 | }]) |
| 66 | 97 | } else if path.is_dir() { |
| 67 | | - // Directory - list all supported files |
| 98 | + // Directory - recursively list all supported files |
| 68 | 99 | 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)?; |
| 93 | 101 | |
| 94 | 102 | // Sort by name |
| 95 | 103 | entries.sort_by(|a, b| a.name.cmp(&b.name)); |