file-browser/src-tauri/src/main.rs

153 lines
4.0 KiB
Rust

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::time::{UNIX_EPOCH};
use std::os::unix::fs::PermissionsExt;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[derive(Debug, serde::Serialize)]
struct EntryMetaData {
name: String,
path: String,
size: u64,
is_directory: bool,
is_file: bool,
is_symlink: bool,
directory_item_count: u64,
permission: String,
created: u64,
modified: u64,
accessed: u64,
}
#[tauri::command]
fn get_files(
_window: tauri::Window,
directory: &str,
) -> Vec<EntryMetaData> {
use std::fs;
use std::path::Path;
let path = Path::new(directory);
// Read the entries in the directory
let entries = fs::read_dir(path).unwrap();
let mut data: Vec<EntryMetaData> = vec![];
for entry in entries {
let entry = entry.unwrap();
let metadata = entry.metadata().unwrap();
let mode = metadata.permissions().mode();
let permission = format!(
"{:04o} ({}{}{})",
mode,
if mode & 0o400 != 0 { 'r' } else { '-' },
if mode & 0o200 != 0 { 'w' } else { '-' },
if mode & 0o100 != 0 { 'x' } else { '-' },
);
let mut directory_item_count = 0 as u64;
if metadata.is_dir() {
if let Ok(dir_entries) = fs::read_dir(entry.path()) {
directory_item_count = dir_entries.count() as u64;
}
}
let mut created = 0 as u64;
if let Ok(created_date) = metadata.created() {
created = created_date.duration_since(UNIX_EPOCH).unwrap().as_secs();
}
let mut modified = 0 as u64;
if let Ok(modified_date) = metadata.modified() {
modified = modified_date.duration_since(UNIX_EPOCH).unwrap().as_secs();
}
let mut accessed = 0 as u64;
if let Ok(accessed_date) = metadata.accessed() {
accessed = accessed_date.duration_since(UNIX_EPOCH).unwrap().as_secs();
}
data.push(EntryMetaData{
name: entry.file_name().to_string_lossy().to_string(),
path: entry.path().to_string_lossy().to_string(),
size: metadata.len(),
is_directory: metadata.is_dir(),
is_file: metadata.is_file(),
is_symlink: metadata.is_symlink(),
directory_item_count,
permission,
created,
modified,
accessed,
});
}
return data;
}
#[tauri::command]
fn open_file(
_window: tauri::Window,
path: &str,
) -> bool {
use std::process::Command;
use std::env;
let os = env::consts::OS;
match os {
"linux" => {
Command::new("xdg-open").arg(path).output()
.expect("Unable to open file");
return true;
},
"macos" => {
Command::new("open").arg(path).output()
.expect("Unable to open file");
return true;
},
"windows" => {
Command::new("cmd").arg("/C").arg("start").arg(path).output()
.expect("Unable to open file");
return true;
},
_ => {
println!("Unsupported operating system: {}", os);
return false;
}
}
}
fn main() {
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)] // only include this code on debug builds
{
let window = app.get_window("main").unwrap();
window.open_devtools();
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
get_files,
open_file
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}