35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
use std::fs;
|
|
use tauri::command;
|
|
|
|
const IMAGE_EXTS: [&str; 6] = ["jpg", "jpeg", "png", "gif", "bmp", "webp"];
|
|
|
|
#[command]
|
|
fn read_directory_images(dir_path: &str) -> Result<Vec<String>, String> {
|
|
let entries = fs::read_dir(dir_path).map_err(|e| e.to_string())?;
|
|
|
|
let mut images: Vec<String> = entries
|
|
.filter_map(|e| e.ok())
|
|
.map(|e| e.path())
|
|
.filter(|p| p.is_file() && is_image(p.extension()))
|
|
.filter_map(|p| p.to_str().map(String::from))
|
|
.collect();
|
|
|
|
images.sort();
|
|
Ok(images)
|
|
}
|
|
|
|
fn is_image(ext: Option<&std::ffi::OsStr>) -> bool {
|
|
ext.map(|e| IMAGE_EXTS.contains(&e.to_string_lossy().to_lowercase().as_str())).unwrap_or(false)
|
|
}
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
pub fn run() {
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_opener::init())
|
|
.plugin(tauri_plugin_dialog::init())
|
|
.plugin(tauri_plugin_fs::init())
|
|
.invoke_handler(tauri::generate_handler![read_directory_images])
|
|
.run(tauri::generate_context!())
|
|
.expect("error while running tauri application");
|
|
}
|